Search code examples
rstat

Calculating median for each column of grouped data


I have a dataframe that looks like this:

 genotype     DIV3     DIV4 ...
 WT           12.4     15.2
 WT           35.4     35.3
 HET          1.3      1.2
 HET          1.5      5.2

I want to be able to calculate the median of each column for each group, but I'm not sure the best way to do this in R. I would prefer if I didn't have to call the genotype, as this may not remain constant for other datasets.


Solution

  • I find it amazing that noone has suggested aggregate yet, seeing as it is the simple, base R function included for these sorts of tasks. E.g.:

    aggregate(. ~ genotype, data=dat, FUN=median)
    
    #  genotype DIV3  DIV4
    #1      HET  1.4  3.20
    #2       WT 23.9 25.25