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

Thomas Lumley tlumley at u.washington.edu
Fri Mar 11 16:29:28 CET 2005


On Fri, 11 Mar 2005, Christoph Lehmann wrote:

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

You can call aov() or lm() with a multicolumn response variable.
> summary(aov(cbind(y1,y2,y3)~factor(x),data=df))
  Response y1 :
             Df Sum Sq Mean Sq F value Pr(>F)
factor(x)    2  0.187   0.093  0.0735 0.9293
Residuals   27 34.326   1.271

  Response y2 :
             Df Sum Sq Mean Sq F value Pr(>F)
factor(x)    2  0.133   0.066  0.0497 0.9516
Residuals   27 36.107   1.337

  Response y3 :
             Df Sum Sq Mean Sq F value  Pr(>F)
factor(x)    2  6.051   3.026  2.5605 0.09589 .
Residuals   27 31.903   1.182
---
Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1


 	-thomas




More information about the R-help mailing list