In this tutorial, we will learn how to compute rolling mean of a column in a dataframe in R. Rolling mean often useful in time series data analysis is also known as moving average or running average calculates average of data points over window of specified size.
In R, we can use zoo packages rollmeanr() function to compute rolling mean for a window size of interest and also deal with NAs. We will learn how to use rollmeanr() function to compute rolling mean with multiple examples.
library(tidyverse) # install.packages(cow) theme_set(theme_bw(16))
df <- tibble(x = 1:10) df # A tibble: 10 × 1 x <int> 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10
We can compute rolling mean using rollmeanr() function from the R package zoo. rollmeanr() can deal with NAs nicely. For example if we want compute rolling mean with window size 2, we will use rollmeanr() as shown below. The first element is NA as we don’t have window of size two. For all the the other elements, we get the average as we expect.
df %>% mutate(rolling_mean = zoo::rollmeanr(x, k=2, fill = NA)) # A tibble: 10 × 2 x rolling_mean <int> <dbl> 1 1 NA 2 2 1.5 3 3 2.5 4 4 3.5 5 5 4.5 6 6 5.5 7 7 6.5 8 8 7.5 9 9 8.5 10 10 9.5
Here is an example of rolling mean with window size 3 and here we get the first two elements as NAs.
df %>% mutate(rolling_mean = zoo::rollmeanr(x, k=3, fill = NA)) # A tibble: 10 × 2 x rolling_mean <int> <dbl> 1 1 NA 2 2 NA 3 3 2 4 4 3 5 5 4 6 6 5 7 7 6 8 8 7 9 9 8 10 10 9
In the above examples, we computed rolling mean for an element based on the elements above. That is why our NAs were at the top. We can change that and compute rolling mean of an element with window containing elements after the element of interest with align=”left” option.
df %>% mutate(rolling_mean = zoo::rollmeanr(x, k=3, fill = NA, align="left")) # A tibble: 10 × 2 x rolling_mean <int> <dbl> 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 NA 10 10 NA