Search code examples
rmedian

Calculate medians of multiple columns in a data frame


I would like to calculate the median of my df database below. In this case, I would like to get the median results for columns A1 through A10 and return the results for the columns separately.

Thanks!

#database
df <- structure(
 list(D1 = c("a","a","b","b","b"), 
      D2 = c("c","d","c","d","c"), D3 = c("X","X","Y","Z","Z"), A1=c(1,2,3,4,5),A2=c(4,2,3,4,4), A3=c(1,2,3,4,6),
      A4=c(1,9,4,4,6),A5=c(1,4,3,9,6),A6=c(1,2,4,4,8),A7=c(1,1,3,4,7),A8=c(1,6,4,4,2),A9=c(1,2,3,4,6),A10=c(1,5,3,2,7)),
      class = "data.frame", row.names = c(NA, -5L))

Solution

  • We can loop over the numeric columns and get the median

    library(dplyr)
    df %>% 
       summarise(across(where(is.numeric), median))
      A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
    1  3  4  3  4  4  4  3  4  3   3
    

    Or use colMedians from matrixStats

    library(matrixStats)
    colMedians(as.matrix(df[startsWith(names(df), "A")]))
     [1] 3 4 3 4 4 4 3 4 3 3
    

    Or in base R

    sapply(df[startsWith(names(df), "A")], median)
     A1  A2  A3  A4  A5  A6  A7  A8  A9 A10 
      3   4   3   4   4   4   3   4   3   3