statar (version 0.1.1)

roll_lag: Apply rollling functions with respect to a time variable

Description

Apply rollling functions with respect to a time variable

Usage

roll_lag(x, FUN, n, along_with = NULL, order_by = NULL, closed = c(TRUE,
  TRUE), ...)

roll_lead(x, FUN, n, along_with = NULL, order_by = NULL, closed = c(TRUE, TRUE), ...)

Arguments

x
a vector or matrix
FUN
function to apply on x
n
an integer specifying the rolling window
along_with
use this variable to roll the function based on the [along_with - n, along_with] rather than past n rows. NA are not accepted
order_by
override the default ordering to use another vector
closed
Logical of length 2 (recycled) Should interval be closed ? Default to c(TRUE, TRUE)
...
options to pass to the function FUN

Examples

Run this code
along_with  = c(1, 2, 4, 7)
x <- c(1, 1, 1, 1)
roll_lag(x,sum, n = 1, along_with = along_with)
roll_lag(x, sum, n = 1, along_with = along_with)
roll_lag(x, sum, n = 2, along_with = along_with)
roll_lead(x, sum, n = 1, along_with = along_with)
roll_lead(x, sum, n = 2, along_with = along_with)
y <- c(1, 2, 1, 1)
roll_lag(list(x,y), function(z){cov(z[[1]], z[[2]])},  n = 2, along_with = along_with)

Run the code above in your browser using DataLab