I am trying to figure out how to smooth data that I've averaged in to "day of year" data. I have simplified the problem in the example code below to the minimum possible. In my actual script I have calculated a data frame that has a "day of year" index column from 1 to 365 and a 2nd column that is the average of a specific measurement for that day of the year over many years. I seek to further smooth the data by calculating a centered running average (of, for example 11 days) on this data.
I am having a hard figuring out how to efficiently handle the "calendar break", meaning that at the beginning and end of the "day of year" data I need to cross DoY = 365 back to DoY=1. How do I calculate the running average when the center of the average runs from day=360 to day=5?
I started to kluge together a solution but quickly arrived at less than elegant code. Is there an efficient means to do this?
The example below provides an example data frame with trial data.
# A simulated daily time series average
ann_data <- data.frame(day=seq(1,365,1), data=
(sin(pi*seq(1:365)/182+90)+rnorm(365)/10))
plot(ann_data)
ann_data_smooth <- ?