Moving average of previous three values in R
moving-average, r, zoo
Solution
A simplier implementation of w_i_l_l's mavback function based on his mav function
`mavback <- function(x,n){ filter(x, c(0, rep(1/n,n)), sides=1) }`
Problem
In the zoo package there is a function called rollmean, which enables you to make moving averages. The `rollmean(x,3)` will take the previous, current and next value (ie 4, 6 and 2) in the table below. This is shown in the second column. ``` x rollmean ma3 4 6 4.0 2 4.3 5 3.0 4.0 2 6.3 4.3 12 6.0 3.0 4 6.0 6.3 2 6.0 ``` I would like to get the same job done, but by averaging out the previous 3 values in the fourth row. This is displayed in the third column. Can anybody tell me the name of the function that will help to accomplish this?