Re: [R] aov or t-test applied on all variables of a data.frame

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 12 Mar 2005 - 02:47:27 EST

Christoph Lehmann <christoph.lehmann@gmx.ch> writes:

> Hi
> I have a data.frame with say 10 continuous variables and one grouping
> factor (say 3 levels)
>
> how can I easily (without loops) apply for each continous variable
> e.g. an aov, with the grouping factor as my factor (or if the grouping
> factor has 2 levels, eg. a t-test)
>
> thanks for a hint

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]))

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon Mar 14 09:57:39 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:43 EST