[R] What is the best method to produce means by categorical factors?
John Kane
jrkrideau at yahoo.ca
Fri Jul 31 01:09:37 CEST 2009
The most common would be aggregate. You can change mean for other functions ( e.g. sum or length, median, or one you write yourself)
You probably would find Bob Munechen's book http://rforsasandspssusers.com/ very useful and there is a shorter pdf available on that page.
Example:
dd <- data.frame(factor1= rep(letters[1:10], 2), factor2=rep(LETTERS[1:5],4),
var1=rnorm(20, 10,2), var2=rnorm(20, 25,5))
aggregate(dd[,3:4], by=list(dd[,1],dd[,2]), mean)
aggregate(dd[,3:4], by=list(dd[,1],dd[,2]), sum)
--- On Thu, 7/30/09, Pat Schmitz <p.schmitz at gmail.com> wrote:
> From: Pat Schmitz <p.schmitz at gmail.com>
> Subject: [R] What is the best method to produce means by categorical factors?
> To: r-help at r-project.org
> Received: Thursday, July 30, 2009, 4:19 AM
> 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?
>
>
>
>
>
>
> --
> 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.
>
__________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now
http://ca.toolbar.yahoo.com.
More information about the R-help
mailing list