1

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!

JayPeerachai
  • 3,499
  • 3
  • 14
  • 29
A. M
  • 23
  • 4
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 03 '20 at 19:44
  • Thanks, just edited with clarification. – A. M Sep 03 '20 at 19:59

1 Answers1

1

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
DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • Many thanks! This works. Can you explain why just using sapply on the data doesn't work, and the difference here? – A. M Sep 03 '20 at 20:38
  • 1
    Because you’ve got two sets of things you’re trying to loop over - the different methods and the different columns of the data. – DaveArmstrong Sep 03 '20 at 20:58