Search code examples
rstatisticst-test

R code for unpaired t-test across multiple columns


I am looking to adjust the p-value (fdr method) in the following code:

>lstOut <- combn(unique(df1$ATTRIBUTE_Group), 2, FUN = function(x) {+ dat1 <- subset(df1, ATTRIBUTE_Group == x[1])+ dat2 <- subset(df1, ATTRIBUTE_Group == x[2])+ Map(function(x, y) tryCatch(t.test(x, y,var.equal=TRUE), error = function(e) NA), dat1[-1], dat2[-1])+ }, simplify = FALSE)
>names(lstOut) <- combn(as.character(unique(df1$ATTRIBUTE_Group)), 2, FUN = paste, collapse="_")
>out <- map_depth(lstOut, .depth = 2, tidy)
>out2 <- map_dfr(out, ~bind_rows(.x, .id = 'colname'), .id = 'classCompare')

Thanks so much, L.


Solution

  • We can use p.adjust

    library(dplyr)
    out3 <- out2 %>%
                  group_by(colname) %>%
                  mutate(padj = p.adjust(p.value, method = "BH"))