Skip to content
Prev 65783 / 398503 Next

aov or t-test applied on all variables of a data.frame

Christoph Lehmann <christoph.lehmann at gmx.ch> writes:
Generally something with lapply or sapply, e.g.

lapply(dd[-1], function(y) t.test(y~dd$V1))

$V2

        Welch Two Sample t-test

data:  y by dd$V1
t = 1.5465, df = 39.396, p-value = 0.13
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.02500802  0.18764439
sample estimates:
mean in group 1 mean in group 2
       1.096818        1.015500

...etc, one for each of V2..V8

or, in a more compact form 

sapply(dd[-1], function(y) t.test(y~dd$V1))[1:3,]

          V2        V3        V4         V5         V6        V7
statistic 1.546456  1.008719  0.08158578 -0.2456436 -0.872376 -1.405966
parameter 39.39554  36.30778  39.70288   36.99061   36.99944  35.97947
p.value   0.1299909 0.3197851 0.935386   0.807316   0.3886296 0.1683118
          V8
statistic -0.6724112
parameter 29.65156
p.value   0.5065284

or (this'll get the confidence intervals and estimates printed sensibly).

sapply(dd[-1], function(y)unlist(t.test(y~dd$V1)[1:5]))