[R] Odp: What is the best method to produce means by categorical factors?

Petr PIKAL petr.pikal at precheza.cz
Thu Jul 30 10:35:24 CEST 2009


Hi

r-help-bounces at r-project.org napsal dne 30.07.2009 10:19:21:

> I am attempting to replicate some of my experience from SAS in R and 
assume
> there are best methods for using a combination of summary(), subset, and
> which() to produce a subset of mean values by categorical or ordinal
> factors.
> 
> within sas I would write
> 
> proc means mean data=dataset;
> class factor1 factor2
> var variable1 variable2;
> RUN;
> 
> producing an output with means for each variable by factor groupings as
> below:
> 
> *factor1        factor2          obs       variable    mean*
> Level A        treatmentA        3        variable1    10
>                                           variable2    22
> 
>                treatmentB        3        variable1    12
>                                           variable2    30
> 
> Level B        treatmentA        3        variable1    10
>                                           variable2    22
> 
>                treatmentB        3        variable1    12
>                                           variable2    30
> 
> What is the best way to go about this in R?

See ?aggregate, ?by, ?tapply and maybe also doBy and plyr packages.

Something like
aggregate(data, list(variable, factor2, factor1), mean)

Best regards
Petr



> 
> 
> 
> 
> 
> 
> -- 
> Patrick Schmitz
> Graduate Student
> Plant Biology
> 1206 West Gregory Drive
> RM 1500
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list