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

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Jul 30 10:39:03 CEST 2009


Dear Pat,

Have a look at recast from the reshape package.

library(reshape)
dataset <- expand.grid(factor1 = c("A", "B"), factor2 = c("C", "D"), Rep
= 1:3)
dataset$variable1 <- rnorm(nrow(dataset))
dataset$variable2 <- rnorm(nrow(dataset), mean = 10)
recast(factor1 + factor2 + variable ~ ., data = dataset, id.var =
c("factor1", "factor2", "Rep"), fun = mean) 

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Pat Schmitz
Verzonden: donderdag 30 juli 2009 10:19
Aan: r-help at r-project.org
Onderwerp: [R] What is the best method to produce means by categorical
factors?

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.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




More information about the R-help mailing list