Re: [R] Calculating mean together with split

From: hadley wickham <h.wickham_at_gmail.com>
Date: Wed 20 Sep 2006 - 15:39:28 GMT

> It contains about 19000 entries and the structure looks like this:
>
> NoPlants sim run year DensPlants
> 1 6 lng_cs99_renosterbos 1 4 0.00192
> .
> .
> .
>
> it has 43 different entries for sim and year goes from 1 to 100, and run
> from 1 to 5.
>
> I would like to calculate the mean of DensPlants for each simulation and
> each year seperately, i.e. calculating the mean for all combinations of
> sim and year over run.

You can do this pretty easily with the reshape package:

library(reshape)
dfm <- rename(df, c(DensPlants = value)) # this is the form that reshape wants

# Then try one of these:

cast(dfm, year ~ sim)
cast(dfm, year + sim ~ . )
cast(dfm, year ~ sim, margins=TRUE)

Depending on what format you want the resulting summaries in.

Hadley



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. Received on Thu Sep 21 01:50:21 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 20 Sep 2006 - 16:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.