0

I have a dataframe consisting of multiple columns (each of them is a different variable) and the first column is the "Group", which indicates controls and patients. How can I perform 2-sample t-tests for all the columns (variables) based on the two different groups in the first column using R?

  • Please provide a reproducible example. Do you want all pairs of columns t-tested against each other, so for `n` columns you need `n(n-1)` tests, or each column t-tested against a reference, so `n-1` tests? – PeterK May 02 '22 at 15:11

1 Answers1

0

Use mapply. I will exemplify with the built-in data set iris. Its last column, Species, is a factor with 3 levels, 50 elements per level, so I'll keep the first 100 rows and change the columns order.

df1 <- iris[1:100, c(5, 1:4)]
ttest_list <- mapply(\(x, g) t.test(x ~ g), df1[-1], df1[1], SIMPLIFY = FALSE)

ttest_list[[1]]
#> 
#>  Welch Two Sample t-test
#> 
#> data:  x by g
#> t = -10.521, df = 86.538, p-value < 2.2e-16
#> alternative hypothesis: true difference in means between group setosa and group versicolor is not equal to 0
#> 95 percent confidence interval:
#>  -1.1057074 -0.7542926
#> sample estimates:
#>     mean in group setosa mean in group versicolor 
#>                    5.006                    5.936

Created on 2022-05-02 by the reprex package (v2.0.1)

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66