How to write a loop to run the t-test of a data frame?
Here's a simple solution, which doesn't require additional packages:
lapply(testData[-1], function(x) t.test(x ~ testData$Label))
Here testData[-1]
refers to all columns of testData
but the first one (which contains the labels). Negative indexing is used for excluding data.
You can use the formula interface to t.test
and use lapply
to iterate along the column names to build the formulae:
lapply(names(testData)[-1],function(x)
t.test(as.formula(paste(x,"Label",sep="~")),
data=testData))
[[1]]
Welch Two Sample t-test
data: F1 by Label
t = -3.6391, df = 13.969, p-value = 0.002691
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.4519374 -0.1167204
sample estimates:
mean in group Bad mean in group Good
0.3776753 0.6620042
[[2]]
Welch Two Sample t-test
data: F2 by Label
t = 3.7358, df = 12.121, p-value = 0.002796
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
0.06997617 0.26529723
sample estimates:
mean in group Bad mean in group Good
0.8008989 0.6332622
...
I put the data in a long format , using reshape2
then I use your code but within a lapply
.
library(reshape2)
dat <- melt(testData)
lapply(unique(dat$variable),function(x){
Good <- subset(dat, Label == 'Good' & variable ==x)$value
Bad <- subset(dat, Label == 'Bad' & variable ==x)$value
t.test(Good,Bad)
})