I'm trying to do a smoothed mean on weeks, but need to do it on the +/- 1 week before and after the target week, and the overlap from week 52/53 to week 1 is giving me a few issues. I can hardcode it with a for loop and a few if statements and make it work, but I'm convinced there is a more elegant solution.
I'm looking for a data.frame output with two columns, weeks ranging 1:53 and where value is a smoothed mean. For week 1 it should average week 53, 1 and 2. For week 53 the average should consist of the values in week 52, 53 and 1. It would be great if it could be used as a summary function after a dplyr::group_by pipe.
I have generated a few years of weekly data:
week <- c(1:53, 1:52, 1:52)
tibble(week = week,
value = rnorm(n = length(week)))
Edit: I'm looking for a solution that summarise into distinct weeks, but where the mean also includes the neighboring weeks.