I'm attempting to find the 7-day rolling average of new cases and new deaths in a COVID-19 dataset as part of an assignment. I've already found the new cases and deaths per day. I just need to find the 7-day average for each day that there's enough data. I want to do this as a new column using the mutate function. However, when I put the function in, the data that shows up begins at day 4, not 7, and is mathematically incorrect. Any ideas?
I have tried the below code, and I should be getting these results:
us_totals %>%
mutate(delta_deaths_7 = (rollmean(deaths, k = 7, fill = NA)))
which gets:incorrect code
I know for a fact that the rolling average for that column is supposed to start on day 7, and the first row should have a value of 55.7.
I have also tried the slider function, a self-input function to calculate the rolling mean, and all possible alignments of the rollmean function.
So far, everything has either yielded nothing but NA values, or the code seen above.
Some added detail: the code has been grouped by date already, and filtered to only pull data since March 15th, 2020. I realize that the rollmean could be pulling from filtered-out data but have no clue how to fix it.