计算R中每个matrix的平均值

我在R工作室工作。 我需要计算数据框的每列的平均值。

cluster1 // 5 by 4 data frame mean(cluster1) // 

我有 :

  Warning message: In mean.default(cluster1) : argument is not numeric or logical: returning NA 

但我可以使用

  mean(cluster1[[1]]) 

得到第一列的平均值。

如何获得所有列的手段?

任何帮助,将不胜感激。

你可以使用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 

你可以使用'apply'来运行一个函数或matrix或数字数据框的行或列:

 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 

另一种方法是使用呜声

 # example data like what is said above 

@手推车和马海毛

 set.seed(1) m <- data.frame(matrix(sample(100, 20, replace = TRUE), ncol = 4)) library(purrr) means <- map_dbl(m, mean) > means # X1 X2 X3 X4 #47.0 64.4 44.8 67.8 

如果你有NA:

 sapply(data, mean, na.rm = T) # Returns a vector (with names) lapply(data, mean, na.rm = T) # Returns a list 

请记住,“意思”需要数字数据。 如果您有混合的类数据,则使用:

 numdata<-data[sapply(data, is.numeric)] sapply(numdata, mean, na.rm = T) # Returns a vector lapply(numdata, mean, na.rm = T) # Returns a list