请考虑以下示例:
Factor <- c(rep('Male', 10),rep('Female', 10))
Age <- sample(30:80,20)
df1 <- data.frame(Factor, Age)
with(df1, tapply(Age, Factor, mean))最后命令给出了男女的平均年龄。现在,假设一个inout标记为NA。我们怎样才能克服这个问题?
df1$Age[15] <- NA
with(df1, tapply(Age, Factor, mean))发布于 2017-01-19 17:29:06
您可以传递tapply中使用的函数的参数,在本例中是mean。
如果您查看?mean,您将看到mean的默认设置是na.rm = False。只要改变它:
tapply(df1$Age, df1$Factor, mean, na.rm = T)或者,使用with
with(df1, tapply(Age, Factor, mean, na.rm = T))https://stackoverflow.com/questions/41747762
复制相似问题