You can use rollmean
, but set align='right'
. Or you could use rollmeanr
, which has align='right'
as the default.
ma3 <- rollmeanr(x[,1],3,fill=NA)
…but you would still need to lag the result. Another solution is to use rollapply
with a list for the width
argument:
ma3 <- rollapplyr(x[,1],list(-(3:1)),mean,fill=NA)