library(tidyverse)
library(purrr)
library(zoo)
data %>%
group_by(group_name) %>%
nest() %>%
mutate(data = pmap(list(data),
~ mutate(.x,
r_mean = lag(rollmean(x = var_name, k = 7, fill = NA, align = "right")))
)
) %>%
unnest(cols = data)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter