Skip to main content
added 470 characters in body
Source Link
StupidWolf
  • 5.2k
  • 3
  • 14
  • 28
library(genefilter)
set.seed(111)
exampleDF <- data.frame(s1 = rnorm(10), s2 = rnorm(10), s3 = rnorm(10), s4 = rnorm(10), s5 = rnorm(10))
group = factor(rep(c("A","B"),c(3,2)))

res = rowttests(as.matrix(exampleDF),factor(group))
res$adjP = p.adjust(res$p.value,"BH")
head(res)
     statistic          dm    p.value
1  -0.04874235 -0.06430958 0.96418815
2   2.01047763  adjP
1.30645511 0.13791972
3 3.04099375  02.86318365 2855413 0.8687078505582346 0.451523563261168
4 2  02.6199888384786452  0.309025049011630 0.57917127
5  06522336 10.21511532 3261168
3 1.00878255 0.31123065
6 41773686  0.07756532 5554058 0.0882825070423693 0.943057438802962
7 4 -0.9805698909067738 -0.661758141196167 0.39911481
8  93346409 0.90571157 9804451
5 0.98698010 0.43185491
9 91684108  31.16533692 0797461 10.1492354942683640 0.050662287113940
10 6 -0.9137026891749882 -0.8714534 0.8299524842654148 0.428246197113940

The default in rowttest assumes equal variance for groups. You have to see whether this is true. If you need exact p-valuesthis is not the case, thenyou can go back to using t.test()

Then it will be:

library(broom)

res = apply(exampleDF,1,function(i)tidy(t.test(i ~ group)))
res = do.call(rbind,res)
res$adjP = p.adjust(res$p.value,"BH")

res[,c("statistic","p.value","adjP")]
# A tibble: 10 x 3
   statistic p.value  adjP
       <dbl>   <dbl> <dbl>
 1    2.33    0.248  0.826
 2    3.52    0.0515 0.515
 3    0.364   0.762  0.952
 4   -0.0897  0.936  0.976
 5    0.706   0.602  0.952
 6   -1.00    0.393  0.952
 7   -2.15    0.135  0.675
 8   -0.0341  0.976  0.976
 9   -0.983   0.488  0.952
10   -0.471   0.710  0.952
library(genefilter)
set.seed(111)
exampleDF <- data.frame(s1 = rnorm(10), s2 = rnorm(10), s3 = rnorm(10), s4 = rnorm(10), s5 = rnorm(10))
group = factor(rep(c("A","B"),c(3,2)))

res = rowttests(as.matrix(exampleDF),factor(group))
res$adjP = p.adjust(res$p.value,"BH")
head(res)
     statistic          dm    p.value
1  -0.04874235 -0.06430958 0.96418815
2   2.01047763  1.30645511 0.13791972
3   0.86318365  0.86870785 0.45152356
4   0.61998883  0.30902504 0.57917127
5   1.21511532  1.00878255 0.31123065
6   0.07756532  0.08828250 0.94305743
7  -0.98056989 -0.66175814 0.39911481
8   0.90571157  0.98698010 0.43185491
9   3.16533692  1.14923549 0.05066228
10  0.91370268  0.82995248 0.42824619

If you need exact p-values, then it will be:

library(broom)

res = apply(exampleDF,1,function(i)tidy(t.test(i ~ group)))
res = do.call(rbind,res)
res$adjP = p.adjust(res$p.value,"BH")
library(genefilter)
set.seed(111)
exampleDF <- data.frame(s1 = rnorm(10), s2 = rnorm(10), s3 = rnorm(10), s4 = rnorm(10), s5 = rnorm(10))
group = factor(rep(c("A","B"),c(3,2)))

res = rowttests(as.matrix(exampleDF),factor(group))
res$adjP = p.adjust(res$p.value,"BH")
head(res)
    statistic         dm    p.value      adjP
1  3.04099375  2.2855413 0.05582346 0.3261168
2  2.84786452  0.9011630 0.06522336 0.3261168
3  0.41773686  0.5554058 0.70423693 0.8802962
4 -0.09067738 -0.1196167 0.93346409 0.9804451
5  0.91684108  1.0797461 0.42683640 0.7113940
6 -0.91749882 -0.8714534 0.42654148 0.7113940

The default in rowttest assumes equal variance for groups. You have to see whether this is true. If this is not the case, you can go back to using t.test()

Then it will be:

library(broom)

res = apply(exampleDF,1,function(i)tidy(t.test(i ~ group)))
res = do.call(rbind,res)
res$adjP = p.adjust(res$p.value,"BH")

res[,c("statistic","p.value","adjP")]
# A tibble: 10 x 3
   statistic p.value  adjP
       <dbl>   <dbl> <dbl>
 1    2.33    0.248  0.826
 2    3.52    0.0515 0.515
 3    0.364   0.762  0.952
 4   -0.0897  0.936  0.976
 5    0.706   0.602  0.952
 6   -1.00    0.393  0.952
 7   -2.15    0.135  0.675
 8   -0.0341  0.976  0.976
 9   -0.983   0.488  0.952
10   -0.471   0.710  0.952
Source Link
StupidWolf
  • 5.2k
  • 3
  • 14
  • 28

First of all, if you have only 3 + 2 samples in each group, you are quite inaccurate in estimating the standard error, and doing this 20000x t.test is most not likely not the best approach. If your data is something related to gene expression or biological data, my suggestion is to check out the R bioconductor package limma and also this paper.

If you absoluately want to do a t.test, below is a quick method, using a library and the p-values are approximate:

library(genefilter)
set.seed(111)
exampleDF <- data.frame(s1 = rnorm(10), s2 = rnorm(10), s3 = rnorm(10), s4 = rnorm(10), s5 = rnorm(10))
group = factor(rep(c("A","B"),c(3,2)))

res = rowttests(as.matrix(exampleDF),factor(group))
res$adjP = p.adjust(res$p.value,"BH")
head(res)
     statistic          dm    p.value
1  -0.04874235 -0.06430958 0.96418815
2   2.01047763  1.30645511 0.13791972
3   0.86318365  0.86870785 0.45152356
4   0.61998883  0.30902504 0.57917127
5   1.21511532  1.00878255 0.31123065
6   0.07756532  0.08828250 0.94305743
7  -0.98056989 -0.66175814 0.39911481
8   0.90571157  0.98698010 0.43185491
9   3.16533692  1.14923549 0.05066228
10  0.91370268  0.82995248 0.42824619

If you need exact p-values, then it will be:

library(broom)

res = apply(exampleDF,1,function(i)tidy(t.test(i ~ group)))
res = do.call(rbind,res)
res$adjP = p.adjust(res$p.value,"BH")