rdataframeapplybonferroni

In R, how can I use all methods of p.adjust on a dataframe in one go?


At the moment I have a data frame of p values.

l<- data.frame(p1 = c(0.01,0.5,0.6), p2= c(0.04,0.9,0.02))

and I would like to apply p.adjust using all methods on each column independently of the others.

Below is the only way that seems to work... but only for one column at a time.

library(multcomp)
set.seed(2020)

l<- data.frame(p1 = c(0.01,0.5,0.6), p2= c(0.04,0.9,0.02))
p.adjust.M<- p.adjust.methods
sapply(p.adjust.M, function(meth) p.adjust( l[,1], meth))
`p.adjust.M<- p.adjust.methods`

The output below is for the first column. I would ideally like to apply all the methods below to all columns in one go and have n dataframes corresponding to n columns of adjustments.

     holm hochberg hommel bonferroni   BH    BY  fdr none
[1,] 0.03     0.03   0.03       0.03 0.03 0.055 0.03 0.01
[2,] 1.00     0.60   0.60       1.00 0.60 1.000 0.60 0.50
[3,] 1.00     0.60   0.60       1.00 0.60 1.000 0.60 0.60

Unfortunately I am going to have a lot of columns so this isn't a feasible approach.

Thanks in advance!


Solution

  • You could wrap the whole sapply() function inside an lapply() function where the data (first argument) is your dataset, like below:

    lapply(l, function(L)sapply(p.adjust.M, function(meth) p.adjust( L, meth)))
    # $p1
    # holm hochberg hommel bonferroni   BH    BY  fdr none
    # [1,] 0.03     0.03   0.03       0.03 0.03 0.055 0.03 0.01
    # [2,] 1.00     0.60   0.60       1.00 0.60 1.000 0.60 0.50
    # [3,] 1.00     0.60   0.60       1.00 0.60 1.000 0.60 0.60
    # 
    # $p2
    # holm hochberg hommel bonferroni   BH   BY  fdr none
    # [1,] 0.08     0.08   0.08       0.12 0.06 0.11 0.06 0.04
    # [2,] 0.90     0.90   0.90       1.00 0.90 1.00 0.90 0.90
    # [3,] 0.06     0.06   0.06       0.06 0.06 0.11 0.06 0.02