How to perform paired t test for multiple columns in R?

R ProgrammingServer Side ProgrammingProgramming

When we have a factor column in an R data frame that has two levels and multiple numerical columns then we can apply paired-test on this data frame but the data must be collected for same subjects, otherwise it will not be a paired data. The t.test application on the data discussed here can be done by using the command lapply(df[-1], function(x) t.test(x~df$group)), where group is the factor column and lies at the first position in the data frame, x contains all the numerical columns in the data frame, and all these columns are stored in data frame called df.

Example

Consider the below data frame −

 Live Demo

x1<-sample(c("A","B"),20,replace=TRUE)
y1<-rpois(20,5)
y2<-rpois(20,2)
y3<-rpois(20,10)
y4<-rpois(20,3)
y5<-rpois(20,2)
df<-data.frame(x1,y1,y2,y3,y4,y5)
df

Output

  x1  y1 y2 y3 y4 y5
1  A  5  0  9  3  0
2  B  4  1  6  1  1
3  B  4  2 10  1  1
4  A  3  0 13  2  1
5  A  6  1 10  0  2
6  A  6  1 16  4  2
7  A 10  3  9  5  1
8  B  5  0 16  0  2
9  B  2  1 10  3  3
10 B  3  2 11  2  3
11 A  3  1  9  1  2
12 B  7  1  8  2  2
13 A  6  0  4  0  5
14 B  9  2 15  1  5
15 A  4  1  8  1  2
16 B  7  0 11  3  2
17 B  2  5  9  3  2
18 A  7  2 14  3  2
19 B  3  3 13  0  0
20 A  5  2  9  3  5

Applying paired t.test on multiple columns of data frame df1 with factor column x1 −

Example

lapply(df[-1], function(x) t.test(x~df$x1))

Output

$y1
   Welch Two Sample t-test
data: x by df$x1
t = 0.90555, df = 17.683, p-value = 0.3773
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.190729 2.990729
sample estimates:
mean in group A mean in group B
5.5 4.6
$y2
   Welch Two Sample t-test
data: x by df$x1
t = -1.057, df = 15.664, p-value = 0.3065
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.8054596 0.6054596
sample estimates:
mean in group A mean in group B
1.1 1.7
$y3
   Welch Two Sample t-test
data: x by df$x1
t = -0.55089, df = 17.802, p-value = 0.5886
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.853392 2.253392
sample estimates:
mean in group A mean in group B
10.1 10.9
$y4
   Welch Two Sample t-test
data: x by df$x1
t = 0.92338, df = 16.062, p-value = 0.3695
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.7770541 1.9770541
sample estimates:
mean in group A mean in group B
2.2 1.6
$y5
   Welch Two Sample t-test
data: x by df$x1
t = 0.14907, df = 17.521, p-value = 0.8832
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.312111 1.512111
sample estimates:
mean in group A mean in group B
2.2 2.1
raja
Published on 16-Mar-2021 12:28:33
Advertisements