roll_lag: Fast rolling grouped lags and differences

View source: R/roll_lag.R

roll_lagR Documentation

Fast rolling grouped lags and differences


Inspired by 'collapse', roll_lag and roll_diff operate similarly to flag and fdiff.


roll_lag(x, n = 1L, ...)

## Default S3 method:
roll_lag(x, n = 1L, g = NULL, fill = NULL, ...)

roll_diff(x, n = 1L, ...)

## Default S3 method:
roll_diff(x, n = 1L, g = NULL, fill = NULL, differences = 1L, ...)

  n = 1L,
  differences = 1L,
  order = NULL,
  run_lengths = NULL,
  fill = NULL



A vector or data frame.


Lag. This will be recycled to match the length of x and can be negative.


Arguments passed onto appropriate method.


Grouping vector. This can be a vector, data frame or GRP object.


Value to fill the first n elements.


Number indicating the number of times to recursively apply the differencing algorithm. If length(n) == 1, i.e the lag is a scalar integer, an optimised method is used which avoids recursion entirely. If length(n) != 1 then simply recursion is used.


Optionally specify an ordering with which to apply the lags/differences. This is useful for example when applying lags chronologically using an unsorted time variable.


Optional integer vector of run lengths that defines the size of each lag run. For example, supplying c(5, 5) applies lags to the first 5 elements and then essentially resets the bounds and applies lags to the next 5 elements as if they were an entirely separate and standalone vector.
This is particularly useful in conjunction with the order argument to perform a by-group lag.


While these may not be as fast the 'collapse' equivalents, they are adequately fast and efficient.
A key difference between roll_lag and flag is that g does not need to be sorted for the result to be correct.
Furthermore, a vector of lags can be supplied for a custom rolling lag. For time-based lags, see time_lag.

roll_diff() silently returns NA when there is integer overflow. Both roll_lag() and roll_diff() apply recursively to list elements.


A vector the same length as x.



x <- 1:10

roll_lag(x) # Lag
roll_lag(x, -1) # Lead
roll_diff(x) # Lag diff
roll_diff(x, -1) # Lead diff

# Using cheapr::lag_sequence()
# Differences lagged at 5, first 5 differences are compared to x[1]
roll_diff(x, cheapr::lag_sequence(length(x), 5, partial = TRUE))

# Like diff() but x/y instead of x-y
quotient <- function(x, n = 1L){
  x / roll_lag(x, n)
# People often call this a growth rate
# but it's just a percentage difference
# See ?roll_growth_rate for growth rate calculations

timeplyr documentation built on June 22, 2024, 10:01 a.m.