statar (version 0.3.0)

roll_lag: Apply rolling functions within time intervals

Description

Apply rolling functions within time intervals

Usage

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

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

Arguments

x
a vector or matrix
FUN
function to apply on x
n
a numeric specifying the rolling window
along_with
use this variable to roll the function based on the [along_with - n, along_with] rather than the 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)
min
An integer. Return NA if the number of observations is strictkly lower than min
...
options to pass to the function FUN

Examples

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

Run the code above in your browser using DataLab