Find median of every row using matrixStats::rowMedians

后端 未结 2 646
粉色の甜心
粉色の甜心 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:00

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

    df$median_score <- apply(df[,-1], 1, median)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题