3 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
你可以使用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貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用'apply'來運(yùn)行函數(shù)或矩陣或數(shù)值數(shù)據(jù)框的行或列:
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 關(guān)注
- 14488 瀏覽
添加回答
舉報(bào)