I am trying to calculate the row median for a data frame df
with rowMedians
from matrixStats
package.
Abundance Sample
I think you need to remove first column because it's not numeric :
df$median_score <- apply(df[,-1], 1, median)
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.
matrix(df)
is?df
. So rowMedians(df)
gives you the same error as if nothing has happened;as.matrix(df[-1])
and as.matrix(df)
.Understanding these issues prevents you from getting errors in future.