Find median of every row using matrixStats::rowMedians

后端 未结 2 656
粉色の甜心
粉色の甜心 2020-12-21 14:26

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

Abundance Sample         


        
2条回答
  •  不思量自难忘°
    2020-12-21 15:13

    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.

提交回复
热议问题