Q:

How to add in Forlus R

I have a quick question. I want to take the average of each 12 elements in a series. I have the folowing code:
PPrateav = NULL
for (i in 80) {
    PPrateav[i] = sum(PPrate[1+(i-1)*12:(i*12)])/12
}

I cannot see where the problems is. Thanks.

A:

This will do the trick
library(zoo)
PPrateav <- rollapply(PPrate, width=12, mean, by=12)

See ?rollapply for further details on width and by arguments.
If you really want to use the for loop, then try changing your original loop to this new version:
PPrateav <- NULL
for (i in 1:floor(length(PPrate)/12)) {
  PPrateav[i] = mean(PPrate[(1+(i-1)*12):(i*12)])
}

