Search code examples
rdataframemedian

Calculate median of a row in a dataframe and save to new variable


I have an excel file that looks like this

enter image description here

I load this file into R and then I want to calculate the median of columns 1 to 6, per row and save the result in a new variable. I tried doing the following:

data$C_median <- median(data[1:6], na.rm=TRUE)

This results however in the following error:

Error in median.default(data[1:6], na.rm = TRUE) : need numeric data

I'm not sure how to solve this issue as I believe all data to already be numeric (apart from na values that are removed with na.rm = TRUE).

Note that for calculating the mean I am able to use similarly fashioned code without issue.

data$C_mean <- rowMeans(data[1:6], na.rm=TRUE)

Solution

  • Since you want to calculate the median for each row through column 1 to 6, you can use apply() to make it

    data$median <- apply(data[1:6],1,function(v) median(as.numeric(v),na.rm = T))
    

    Note that the error you encounter indicates that you were using non-numeric type data, so you need to convert them to numeric first via as.numeric()