Hi. Consider a time-series dataset d with two columns value (observed value) and datetime (time of observation):
library(dplyr)
d = tibble(value = c(2, 5, 6, 3, 8), datetime = c("2020-08-20 10:10:10", "2020-08-20 10:10:12", "2020-08-20 10:10:16", "2020-08-20 10:10:17", "2020-08-20 10:10:22"))
I would like to perform rolling calculations on this dataset based on the datetime column. Let's say I want to create another column with a mean value with a rolling window of 5sec. What is the most efficient way to perform this?
So far I came up with a three-step solution - first extract 1st and last datetime value. Then create a datetime vector containing every second between 1st and last value. Then join it with the original dataset & perform rolling calcs. Is there any way how to perform all this in a single %>%? Or is there even an more elegant solution to this?
Thanks.