This question is related to this T-tests across multiple columns or tidy the data.
data:
df <- structure(list(Subject = 1:3, PreScoreTestA = c(30L, 15L, 20L
), PostScoreTestA = c(40L, 12L, 22L), PreScoreTestB = c(6L, 9L,
11L), PostScoreTestB = c(8L, 13L, 12L), PreScoreTestC = c(12L,
7L, 9L), PostScoreTestC = c(10L, 7L, 10L)), class = "data.frame", row.names = c(NA,
-3L))
> df
Subject PreScoreTestA PostScoreTestA PreScoreTestB PostScoreTestB PreScoreTestC PostScoreTestC
1 1 30 40 6 8 12 10
2 2 15 12 9 13 7 7
3 3 20 22 11 12 9 10
Here the OP asks if it is possible to apply t.test
to pairs of columns in wide format dataframe. There is a solution already provided using long format.
However I try to apply the following code as an answer to perform the t.test in wide format.
My code using +
as function (works well):
library(dplyr)
library(stringr)
df %>%
mutate(across(starts_with('PreScore'), ~ . +
get(str_replace(cur_column(), "^PreScore", "PostScore")), .names = "{.col}_TTest")) %>%
rename_at(vars(ends_with('TTest')), ~ str_remove(., "PreScore"))
# gives:
Subject PreScoreTestA PostScoreTestA PreScoreTestB PostScoreTestB PreScoreTestC PostScoreTestC
1 1 30 40 6 8 12 10
2 2 15 12 9 13 7 7
3 3 20 22 11 12 9 10
TestA_TTest TestB_TTest TestC_TTest
1 70 14 22
2 27 22 14
3 42 23 19
Now I thought to change function +
by t.test
(which does not work, I tried many variations):
library(dplyr)
library(stringr)
df %>%
mutate(across(starts_with('PreScore'), ~ . t.test
get(str_replace(cur_column(), "^PreScore", "PostScore")), .names = "{.col}_TTest")) %>%
rename_at(vars(ends_with('TTest')), ~ str_remove(., "PreScore"))
I would like to know:
Is it possible to apply t.test
function to sets of predefined column pairs after across
like it is possible for -
+
/
etc...
Further resources I have been through:
Looping to get t.test result in R using dplyr
dplyr summarise multiple columns using t.test
apply t.test on every consecutive pair of columns of a data.frame