rrollapply

How to use lag() function in dplyr, without the previous n rows?


I'm familiar the lag() function in R, but my unique use case for it this time is I'd like to perform a rolling average with rollapplyr() that doesn't include the immediate prior seven rows of data.

Is this possible if I somehow adjust the code which doesn't seem to do the job?

library(tidyverse)
library(zoo)

stack <- data.frame(
  date = seq(Sys.Date(), Sys.Date() - 99, -1),
  var1 = round(rnorm(100, 8000, 500))
  )

lag <- stack %>%
  mutate(var1_lag = lag(rollapplyr(var1, 28, mean, partial = TRUE) - 28))

Solution

  • In rollapply the width= can be a list holding a vector of offsets where offset 0 is the current position, offset -1 is the position before that and so on.

    Below we assume that what is wanted is to take the mean of the values at the 21 offsets which end at offset -7. This excludes offsets -6, -5, -4, -3, -2, -1 and 0 (current row and previous 6 rows for 7 excluded rows in total). Modify offsets if something different were intended.

    offsets <- list(seq(to = -7, length = 21))
    stack %>%
      mutate(var1_lag = rollapplyr(var1, offsets, mean, partial = TRUE, fill = NA))