rollmean with dplyr and magrittr
May be this helps:
library(dplyr)
library(zoo)
data %>%
group_by(o) %>%
mutate(rM=rollmean(u,3, na.pad=TRUE, align="right"))
If you want to do for both columns, u
and v
fun1 <- function(x) rollmean(x, 3, na.pad=TRUE, align="right")
data %>%
group_by(o) %>%
mutate_each(funs(fun1), u, v)
A more flexible wrapper comes from the rowr
package. This allows for windows of different size within your initial data.
data %>%
group_by(o) %>%
mutate(MEANS = rollApply(u, fun=mean, window=3, align='right'))