[R] Regular repeats

Berend Hasselman bhh at xs4all.nl
Tue Aug 13 22:10:29 CEST 2013


On 13-08-2013, at 18:46, jsf1982 <jamie.freeman at ucl.ac.uk> wrote:

> Hi,
> Many apologies for the simplicity (hopefully!) of this request - I can't
> find it on the forum, but it may have been asked in the past.
> 
> I have a data frame consisting of ~2000 rows. I simply want to take the
> average of the first 6, then the next 6, then the next 6 until the end of
> the table. 
> The command 
> 
> mean(mole[1:6,c("PercentPI")])
> 
> gets me the first 6 rows (column is PercentPI), but I don't know how to
> increase the rows incrementally.


Something like this

N <- 27
dd <- data.frame(A=rnorm(1:N),index=gl(6,6,N))
aggregate(dd$A,by=list(dd$index),mean)

Berend



More information about the R-help mailing list