[R] group means

J.R. Lockwood
2003-02-21T00:00:07Z

> Date: Fri, 21 Feb 2003 11:43:53 +1300
> From: Jeremy Z Butler <jerrytheshrub at hotmail.com>
> To: r-help at stat.math.ethz.ch
> Subject: [R] group means
>
> Hi,
> Any hints on how I would generate the means of each 5 number group in a
> column of numbers in data.frame form. i.e. get mean of first five in column
> and then mean of second five in column etc. etc.
>

One way to do what you want is to create a grouping variable and add
it to your data frame as a factor, and then use tapply. e.g., assuming
your dataframe "d" with column "x" has a number of rows
divisible by
5, use

d$grp<-gl(dim(d)[1]/5,5)
tapply(d$x,d$grp,"mean")

J.R. Lockwood
412-683-2300 x4941
lockwood at rand.org