如何获得一系列值的平均值?

时间:2018-03-13 07:51:11

标签: r

我希望获得以下内容:

mean(meanproportions(excelrange[1],labials),meanproportions(excelrange[2],labials)... meanproportions(excelrange[100],labials))

如何自动填写(...)部分?有没有其他方法可以达到meanproportions的平均值(excelrange [1:100],labials)?

编辑:以下是整个代码。

install.packages("tidyverse")
library(readxl)
labials<-"p|b|m|ʙ|ɸ|β|ɱ|ⱱ|f|v|ʋ|ʘ|ɓ|w|ʍ|ɥ"
coronals<-"t|d|n|r|ɾ|θ|ð|s|z|ʃ|ʒ|ɬ|ɮ|ɹ|l|ʈ|ɖ|ɳ|ɽ|ʂ|ʐ|ɻ|ɭ|ǀ|!|ǂ|ǁ|ɗ|ɕ|ʑ|ɺ|ɧ"
dorsals<-"c|ɟ|ɲ|ç|ʝ|j|ʎ|k|g|ŋ|x|ɣ|ɰ|ʟ|q|ɢ|ɴ|ʀ|χ|ʁ|ʄ|ɠ|ʛ|ɧ|w|ʍ|ɥ"
stops<-"p|b|t|d|ʈ|ɖ|c|ɟ|k|g|q|ɢ|ʔ|ʡ"
fricatives<-"ɸ|β|f|v|θ|ð|s|z|ʃ|ʒ|ʂ|ʐ|ç|ʝ|x|ɣ|χ|ʁ|ħ|ʕ|ʜ|ʢ|ɕ|ʑ|ɧ|ɬ|ɮ|ʍ"
nasals<-"m|ɱ|n|ɳ|ɲ|ŋ|ɴ"
approximants<-"ʋ|ɹ|ɻ|j|ɰ|l|ɭ|ʎ|ʟ|w|ɥ"
high<-"i|y|ɨ|ʉ|ɯ|u"
low<-"a|ɶ|ɑ|ɒ"
front<-"i|y|e|ø|ɛ|œ|æ|a|ɶ"
back<-"ɯ|u|ɤ|o|ʌ|ɔ|ɑ|ɒ"
rounded<-"y|ʉ|u|ʏ|ʊ|ø|ɵ|o|œ|ɞ|ɔ|ɶ|ɒ"
unrounded<-"i|ɨ|ɯ|ɪ|e|ɘ|ɤ|ə|ɛ|ɜ|ʌ|æ|ɐ|a|ɑ"
LJL <- "C:/Users/Ian/OneDrive/Iconicity/Lexicon/Lexicon (LJL).xlsx"
excelrange<-paste0(rep('B'),2:101,rep(':BO'),2:101)
words<-function(x,y){read_excel(LJL,sheet=x,col_names = FALSE,range=y)}
proportion<-function(x,y,z){if(length((nchar(words(x,y))-
nchar(gsub(z,"",words(x,y))))/nchar(words(x,y)))>0){(nchar(words(x,y))-
nchar(gsub(z,"",words(x,y))))/nchar(words(x,y))} else {0}}
number<-function(x,y){if(length(as.integer(!is.na(words(x,y))))>0)
{as.integer(!is.na(words(x,y)))} else {0}}
numbers<-function(y){number(1,y)+number(2,y)+number(3,y)}
proportions<-function(y,z)
{(proportion(1,y,z)+proportion(2,y,z)+proportion(3,y,z))/numbers(y)}
meanproportions<-function(y,z){mean(proportions(y,z),na.rm=TRUE)}

1 个答案:

答案 0 :(得分:0)

你可以这样使用sapply()

mean_proportions <- sapply(X = 1:100, 
                           FUN = function(i) meanproportions(excelrange[i],labials))
mean(mean_proportions)

您还应发布数据并添加空间并正确缩进代码以便更清晰,有关最佳做法,请参阅this