Re: [R] summarizing dataframe at variable/factor levels Try this:

aggregate(dat.ex[2:3], dat.ex[4:5], mean)


On 7/5/07, Afshartous, David  wrote:
>
> All,
>
> Is there an efficient way to apply say "mean" or "median" to a dataframe
>
> according to say all combinations of two variables in the dataframe?
> Below is a simple example and the outline of a "manual" solution that
> will work but is not very efficient
> (could also generalize this to a function).  Searched the archives and
> docs but didn't see anything close to this question.
>
> Cheers,
> dave
>
> dat.ex = data.frame(  rep(c(1:6), each=6), c(rnorm(12), rnorm(12, 1),
> rnorm(12, 2)), rnorm(36, 5), rep(c(1:6), 6),
> rep(c("Drug1", "Drug2", "Placebo"), each=12) )
> names(dat.ex) = c("patient.no", "outcome", "x", "time", "drug")
>
> mean of first 2 time pts on Drug1:
> mean.time.1.drug.1 = mean( dat.ex[dat.ex$time==1 & dat.ex$drug=="Drug1",
> c(2,3)])
> mean.time.2.drug.1 = mean( dat.ex[dat.ex$time==2 & dat.ex$drug=="Drug1",
> c(2,3)])
>
> dat.ex.reduced = as.data.frame(rbind(mean.time.1.drug.1,
> mean.time.2.drug.1))
> dat.ex.reduced$Drug = c("Drug1", "Drug1")  ## add back Drug variable and
> time variable
> dat.ex.reduced$time = c(1,2)
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>

______________________________________________
R-help@stat.math.ethz.ch 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.

