Given the following data:
set.seed(1) data <- data.frame(o=c(\'a\',\'a\',\'a\',\'a\',\'b\',\'b\',\'b\',\'b\',\'c\',\'c\',\'c\',\'c\'), t=c(1,2,3,4
A more flexible wrapper comes from the rowr package. This allows for windows of different size within your initial data.
rowr
data %>% group_by(o) %>% mutate(MEANS = rollApply(u, fun=mean, window=3, align='right'))