• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to Calculate Rolling Mean in R

rstats101 · September 27, 2023 ·

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

Related

Filed Under: rstats101, zoo rollmeanr() Tagged With: rolling mean in R

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version