如何删除R中colMeans()函数的非数值?

时间:2014-01-11 09:44:50

标签: r

这是一行,

colMeans3 <- colMeans(grade3, na.rm=TRUE)

和R说

Error in colMeans(grades3, na.rm = TRUE) : 'x' must be numeric

1 个答案:

答案 0 :(得分:2)

子集grade3仅包含数字变量。

grade3 <- data.frame(a = runif(10), b = rnorm(10), c = letters[1:10])
colMeans(grade3)

find.numeric <- sapply(grade3, is.numeric)
colMeans(grade3[, find.numeric])
        a         b 
0.4675017 0.1642704