I struggled searching for a simple function for moving averages that had some flexibility to do what I needed. I finally wrote a couple functions extending the one based on the filter function which rinni gives above in the comment (but which itself won't work because it will include the current observation in the 3 period average).
Moving average function that includes the current observation
mav <- function(x,n){filter(x,rep(1/n,n), sides=1)}
Moving average function that does not include the current observation
mavback <- function(x,n){ a<-mav(x,1) b<-mav(x,(n+1)) c<-(1/n)*((n+1)*b - a) return(c) }
Backward looking moving average function, not including current obs, based on [h2] readings starting [h1] periods back
mavback1<-function(x,h1,h2){ a<-mavback(x,h1) b<-mavback(x,h1-h2) c<-(1/h2)*(h1*a -(h1-h2)*b) return(c) }