【发布时间】:2013-05-22 02:35:22
【问题描述】:
我想计算内置矩阵state.x77 的“人口”的平均值。代码是:
apply(state.x77[,"Population"],2,FUN=mean)
#Error in apply(state.x77[, "Population"], 2, FUN = mean) :
# dim(X) must have a positive length
我怎样才能防止这个错误?如果我使用$ 符号
apply(state.x77$Population,2,mean)
# Error in state.x77$Population : $ operator is invalid for atomic vectors
什么是原子向量?
【问题讨论】:
-
但无论如何,如果您只是为一列执行此操作,为什么不只是
mean(state.x77[,"Population"])? -
@joran 非常感谢。参考常见问题解答和 mean(state.x77[,"Population"]) 都是有益的。
标签: r