Search code examples
rfunctiondataframemapply

Function to calculate median by column to an R dataframe that is done regularly to multiple dataframes


Trying to write a function to combine multiple steps that are used regularly on an R dataframe. At the moment I stack individual lines, which is most inefficient. An Example each step I take at the moment

library(scores)
MscoreIndex <- 3


labMedians <- mapply(median, df[-1], na.rm = T) #calculate the median for each column except 1st
LabGrandMedian <- median(mapply(median, df[-1], na.rm = T),na.rm = T)
labMscore <- as.vector(round(abs(scores_na(labMedians, "mad")), digits = 2)) #calculate mscore by lab
labMscoreIndex <- which(labMscore > MscoreMax) #get the position in the vector that exceeds Mscoremax
df[-1][labMscoreIndex] <- NA # discharge values above threshold by making NA

An example my df below

structure(list(Determination_No = 1:6, `2` = c(55.94, 55.7, 56.59, 
56.5, 55.98, 55.93), `3` = c(56.83, 56.54, 56.18, 56.5, 56.51, 
56.34), `4` = c(56.39, 56.43, 56.53, 56.31, 56.47, 56.35), `5` = c(56.32, 
56.29, 56.31, 56.32, 56.39, 56.32), `7` = c(56.48, 56.4, 56.54, 
56.43, 56.73, 56.62), `8` = c(56.382, 56.258, 56.442, 56.258, 
56.532, 56.264), `10` = c(56.3, 56.5, 56.2, 56.5, 56.7, 56.5), 
    `12` = c(56.11, 56.46, 56.1, 56.35, 56.36, 56.37)), class = "data.frame", row.names = c(NA, 
-6L))

I started by trying to get the indivdual lab medians and the grandmedian with the following but got errors

I tried.

mediansFunction <- function(x){
              analytemedians <- mapply(median(x[,-1]))
              grandmedian <- median(x[,-1])
              list(analytemedians,grandmedian)
            }

mediansFunction(df)

But I get "Error in median.default(x[, -1]) : need numeric data"


Solution

  • Try :

    mediansFunction <- function(x){
      analytemedians <- sapply(x[-1], median)
      median_of_median <- median(analytemedians)
      grand_median <- median(as.matrix(x[-1]))
      
      list(analytemedians = analytemedians,
           median_of_median = median_of_median,
           grand_median = grand_median)
    }
    
    mediansFunction(df)
    
    #$analytemedians
    #     2      3      4      5      7      8     10     12 
    #55.960 56.505 56.410 56.320 56.510 56.323 56.500 56.355 
    
    #$median_of_median
    #[1] 56.3825
    
    #$grand_median
    #[1] 56.386