Two approaches, based on the tidyverse.
To begin, generate some data as the OP has not provided any test data. From the code sample shown, it seems we have a data.frame in wide format, but we don't know how the columns are named.
library(tidyverse)
library(broom)
ordered_gene <- tibble(
Group=rep(c("low", "medium", "high"), each=5),
Var1=rnorm(15),
Var2=rnorm(15),
Var3=rnorm(15)
)
> ordered_gene
# A tibble: 15 × 4
Group Var1 Var2 Var3
<chr> <dbl> <dbl> <dbl>
1 low -1.21 -0.212 0.792
2 low 0.674 0.0245 2.05
3 low 0.977 -0.792 -0.519
4 low -1.21 -0.687 0.504
5 low -0.478 -0.864 0.620
6 medium 1.87 -1.44 -1.16
7 medium -1.41 0.325 0.687
8 medium 2.72 0.230 -0.887
9 medium 1.72 0.493 -0.346
10 medium 0.752 -1.86 1.11
11 high -0.560 -1.08 -0.376
12 high -0.819 1.44 -0.906
13 high 0.603 0.0608 -0.704
14 high 0.713 2.59 -0.449
15 high 0.128 1.88 1.13
>
The first approach is very awkward. The awkwardness is caused by the fact that the input dataset appears not to be tidy. The second tidies the data and then performs the same analyses, but much more simply.
First approach
lapply(
# For each column in ordered_gene, other than Group...
names(ordered_gene %>% select(-Group)),
function(col) {
ordered_gene %>%
# Keep only the data relating to rowes where group is 'low' or 'high'
filter(Group %in% c("low", "high")) %>%
# Apply the following function
group_map(
function(.x, .y) {
# Convert the output of t.test to a data.frame
tidy(
# Perform the t.test using the 'formula' version of the function
t.test(as.formula(paste0(".x$", col, " ~ .x$Group")))
) %>%
# Identify the column being analysed
add_column(Var=col, .before=1)
}
)
}
) %>%
# Bind the list of tibbles returned by lapply into a single tibble
bind_rows()
# A tibble: 3 × 11
Var estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 Var1 0.263 0.0130 -0.250 0.475 0.649 6.95 -1.05 1.57 Welch Two Sample t-test two.sided
2 Var2 1.48 0.978 -0.506 2.17 0.0870 4.56 -0.323 3.29 Welch Two Sample t-test two.sided
3 Var3 -0.951 -0.260 0.690 -1.74 0.121 7.87 -2.22 0.314 Welch Two Sample t-test two.sided
Second approach
# Tidy the data
tidy_gene <- ordered_gene %>%
pivot_longer(
!Group,
names_to="Var",
values_to="Value"
)
tidy_gene
# A tibble: 45 × 3
Group Var Value
<chr> <chr> <dbl>
1 low Var1 -1.21
2 low Var2 -0.212
3 low Var3 0.792
4 low Var1 0.674
5 low Var2 0.0245
6 low Var3 2.05
7 low Var1 0.977
8 low Var2 -0.792
9 low Var3 -0.519
10 low Var1 -1.21
# … with 35 more rows
tidy_gene %>%
# For each variable
group_by(Var) %>%
# Select only rows where Group is 'low' or 'high'
filter(Group %in% c("low", "high")) %>%
# Perform the t-test
group_modify(~tidy(t.test(as.formula(".x$Value ~ .x$Group"))))
# A tibble: 3 × 11
# Groups: Var [3]
Var estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 Var1 0.263 0.0130 -0.250 0.475 0.649 6.95 -1.05 1.57 Welch Two Sample t-test two.sided
2 Var2 1.48 0.978 -0.506 2.17 0.0870 4.56 -0.323 3.29 Welch Two Sample t-test two.sided
3 Var3 -0.951 -0.260 0.690 -1.74 0.121 7.87 -2.22 0.314 Welch Two Sample t-test two.sided
The two approaches produce identical results