3 回答
TA贡献1850条经验 获得超11个赞
你可以使用colMeans:
### Sample data
set.seed(1)
m <- data.frame(matrix(sample(100, 20, replace = TRUE), ncol = 4))
### Your error
mean(m)
# [1] NA
# Warning message:
# In mean.default(m) : argument is not numeric or logical: returning NA
### The result using `colMeans`
colMeans(m)
# X1 X2 X3 X4
# 47.0 64.4 44.8 67.8
TA贡献1817条经验 获得超6个赞
您可以使用'apply'来运行函数或矩阵或数值数据框的行或列:
cluster1 <- data.frame(a=1:5, b=11:15, c=21:25, d=31:35)
apply(cluster1,2,mean) # applies function 'mean' to 2nd dimension (columns)
apply(cluster1,1,mean) # applies function to 1st dimension (rows)
sapply(cluster1, mean) # also takes mean of columns, treating data frame like list of vectors
- 3 回答
- 0 关注
- 14420 浏览
添加回答
举报