Find median of every row using matrixStats::rowMedians

£可爱£侵袭症+ 提交于 2020-01-05 07:09:09

问题


I am trying to calculate the row median for a data frame df with rowMedians from matrixStats package.

Abundance Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7
Species1   2        4       0       0       0       6       0
Species2   3        5       6       4       0       0       0
Species3   3        7       2       5       8       0       0
Species4   0        0       3       8       0       0       8
Species5   7        5       6       0       0       4       4
Species6   4        2       3       0       0       2       1

I want to calculate the median of every row and append them in a new column. I got an error

Argument 'x' must be a vector or matrix

so I tried to convert my df to a matrix. The str function shows that every value for the species is numeric, so I tried:

library(matrixStats)
matrix(df, rownames.force = NA)
rowMedians(df)

but I am still getting the same error. Any help is appreciated.


回答1:


You don't want to include that Abundance column for median calculation. Let df be your current data frame.

library(matrixStats)
rowMedians(as.matrix(df[-1]))

A few comments besides the correct code above.

  1. Have you checked what matrix(df) is?
  2. Even if it correctly returns you a numeric matrix, it does not overwrite df. So rowMedians(df) gives you the same error as if nothing has happened;
  3. As an exercise, compare as.matrix(df[-1]) and as.matrix(df).

Understanding these issues prevents you from getting errors in future.




回答2:


I think you need to remove first column because it's not numeric :

df$median_score <- apply(df[,-1], 1, median)


来源:https://stackoverflow.com/questions/51525152/find-median-of-every-row-using-matrixstatsrowmedians

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!