[R] functions(t.test) on variables by groups

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Apr 13 14:23:40 CEST 2005


Hai Lin <kevinvol2002 at yahoo.com> writes:

> Dear R users,
> 
> I have a data frame with categorical Vars. "Groups"
> and a couple  columns of numeric Vars. I am trying to
> make two-sample t.test on each variable(s01-s03) by
> Groups.
> 
> A data generated as following:
> 
> zot <- data.frame(Groups=rep(letters[1:2], each=4),
> s01=rnorm(8), s02=rnorm(8), s03=rnorm(8))
> 
> I have written a piece with a for loop. 
> for (i in 1:(length(zot)-1)) {
> 	print(t.test(zot[,i+1]~zot[,1]))
> 	            }
> 
> I wish something can be easier extracted or can save
> it within for loop, or not even using for loop.  

Something like this?

lapply(zot[-1],function(x)t.test(x~zot$Groups))


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




More information about the R-help mailing list