[R] need help creating means table

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Sep 24 22:46:22 CEST 2001


Prof Brian D Ripley <ripley at stats.ox.ac.uk> writes:

> On Mon, 24 Sep 2001, David White wrote:
> 
> > Peter Delgaard wrote:
> > %
> > %How about
> > %
> > %nums <- sapply(xx, is.numeric)
> > %by(xx[nums], list(subjs, cons, vowels), mean)
> > %
> > %?
> > %
> >
> > Thanks Peter, that gets me around the error message, but it return only
> > one value per subjs x vowels x cons combination, I need a separate mean
> > for each of the fix numeric columns.
> 
> That's what mean does on a data frame. You want something like
> 
> by(xx[nums], list(subjs, cons, vowels), function(x) sapply(x, mean))

Thanks Brian. I suspected that, but as there was no sample data to try
it on, I didn't...

Another possiblility:

lapply(xx[nums], function(x) tapply(x, list(subjs, cons, vowels),
                                    mean))

or - same thing, shorter but sneakier:

lapply(xx[nums], tapply, list(subjs, cons, vowels), mean)


-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list