This tutorial will show you how to calculate moving averages in R. You will need the zoo and dplyr package to run this script.

if(!require(zoo)) install.packages("zoo")
if(!require(dplyr)) install.packages("dplyr")

We will create a dataset to use as an example. The dataset will have date column and a numeric column that we will generate randomly. We will set a seed so we can recreate the exact numbers later.

set.seed(7)
start.Date <- as.Date("2022/1/1")
end.Date <- as.Date("2022/4/1")
date.range <- seq.Date(start.Date, end.Date, by = "day")
df <- data.frame("Date" = date.range,
                 "Score" =  runif(length(date.range), 1, 99) )
#show first 10 rows
head(df, n = 10)
##          Date     Score
## 1  2022-01-01 97.913111
## 2  2022-01-02 39.979054
## 3  2022-01-03 12.338382
## 4  2022-01-04  7.835371
## 5  2022-01-05 24.887440
## 6  2022-01-06 78.617022
## 7  2022-01-07 34.326111
## 8  2022-01-08 96.262125
## 9  2022-01-09 17.253837
## 10 2022-01-10 45.992159

Now use the rollmean function to calculate your moving average (you can change the range of the moving average by changing the K arugment). Use the mutate function in order to add rolling average as a new column.

moving_average <- df %>% mutate(moving_average = rollmean(x = Score, k = 7, fill = NA))
head(moving_average, n = 15)
##          Date     Score moving_average
## 1  2022-01-01 97.913111             NA
## 2  2022-01-02 39.979054             NA
## 3  2022-01-03 12.338382             NA
## 4  2022-01-04  7.835371       42.27093
## 5  2022-01-05 24.887440       42.03507
## 6  2022-01-06 78.617022       38.78861
## 7  2022-01-07 34.326111       43.59629
## 8  2022-01-08 96.262125       45.02429
## 9  2022-01-09 17.253837       44.85247
## 10 2022-01-10 45.992159       44.58370
## 11 2022-01-11 17.831312       41.17104
## 12 2022-01-12 23.684756       33.91044
## 13 2022-01-13 76.735571       32.77427
## 14 2022-01-14 10.437551       34.19614
## 15 2022-01-15 45.437881       31.91353