You can use rollmean , but set align='right' . Or you can use rollmeanr , which has align='right' as the default.
ma3 <- rollmeanr(x[,1],3,fill=NA)
... but you still have to lag behind 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)
source share