lag_run {runner} | R Documentation |
Lag dependent on variable
Description
Vector of input lagged along integer vector
Usage
lag_run(x, lag = 1L, idx = integer(0), nearest = FALSE)
Arguments
x |
(vector , data.frame , matrix , xts , grouped_df )
Input in runner custom function f .
|
lag |
(integer vector or single value)
Denoting window lag. If lag is a single value then window lag is constant
for all elements, otherwise if length(lag) == length(x) different window
size for each element. Negative value shifts window forward. One can also
specify lag in the same way as by argument in
base::seq.POSIXt() . See 'Specifying time-intervals' in details
section.
|
idx |
(integer , Date , POSIXt )
Optional integer vector containing sorted (ascending) index of observation.
By default idx is index incremented by one. User can provide index with
varying increment and with duplicated values. If specified then k and
lag are depending on idx . Length of idx have to be equal of length
x .
|
nearest |
logical single value. Applied when idx is used,
then nearest = FALSE returns observation lagged exactly by the
specified number of "periods". When nearest = TRUE
function returns latest observation within lag window.
|
Examples
lag_run(1:10, lag = 3)
lag_run(letters[1:10], lag = -2, idx = c(1, 1, 1, 2, 3, 4, 6, 7, 8, 10))
lag_run(letters[1:10], lag = 2, idx = c(1, 1, 1, 2, 3, 4, 6, 7, 8, 10), nearest = TRUE)
[Package
runner version 0.4.4
Index]