Rolling mean with dplyr

Hi, is it possible to calculate a rolling mean with dplyr? I am thinking the lag function might help here. But not sure how to actually do it. Thanks!

You can try the slider package, it is dplyr friendly

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

I use something like

tibble(time = 1:10, value = rnorm(10)) %>%
  arrange(time) %>%
  mutate(rolling_avg = cumsum(value) / row_number())

The arrange is unnecessary in this example, but in general you want to guarantee a specific ordering.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.