Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save korkridake/f4414248dc1ff64e3b05771ad4dccf46 to your computer and use it in GitHub Desktop.
Save korkridake/f4414248dc1ff64e3b05771ad4dccf46 to your computer and use it in GitHub Desktop.
# Stackoverflow link: https://stackoverflow.com/questions/53267115/rolling-average-using-groupby-and-varying-window-length
# Question: I'm trying to create a rolling average of a column based on an ID column and a measurement time label in R, but I am having a lot of trouble with it.
# Here is what my dataframe looks like:
# ID Measurement Value
#
# A 1 10
#
# A 2 12
#
# A 3 14
#
# B 1 10
#
# B 2 12
#
# B 3 14
#
# B 4 10
df = data.frame(ID = sample(c('A', 'B', 'C', 'D', 'E'), 40, replace = TRUE),
Measurement = sample(c(1, 2, 3, 4), 40, replace = TRUE),
Value = sample(1:500, 40, replace=TRUE))
# The problem is that I have measurement counts varying from 9 to 76 for each ID so I haven't found a solution that will create a column of a rolling average for each ID while handling the varying window length.
library(dplyr)
# Solution 1:
df %>%
group_by(ID) %>%
mutate(rolling_mean = cummean(Value))
# Solution 2:
df %>%
group_by(ID) %>%
mutate(Avrg = cumsum(Value)/(1:n()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment