How to calculate the running mean starting from the 4th column of "datamean" (since the width=4) and have first and last three columns as "NA" or empty?
require(zoo)
data <- zoo(seq(1:10))
datamean <- rollapply(data, width=4, by=1, FUN=mean, align="left")
cbind(data, datamean)
Currently the output is this:
data datamean
1 1 2.5
2 2 3.5
3 3 4.5
4 4 5.5
5 5 6.5
6 6 7.5
7 7 8.5
8 8 NA
9 9 NA
10 10 NA
However I want:
data datamean
1 1 NA
2 2 NA
3 3 NA
4 4 2.5
5 5 3.5
6 6 4.5
7 7 5.5
8 8 NA
9 9 NA
10 10 NA