Vector of input lagged along integer vector
lag_run(x, lag = 1L, idx = integer(0), nearest = FALSE)
(vector
, data.frame
, matrix
, xts
, grouped_df
)
Input in runner custom function f
.
(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.
(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
.
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.
lag_run(1:10, lag = 3)
#> [1] NA NA NA 1 2 3 4 5 6 7
lag_run(letters[1:10], lag = -2, idx = c(1, 1, 1, 2, 3, 4, 6, 7, 8, 10))
#> [1] "e" "e" "e" "f" NA "g" "i" NA "j" NA
lag_run(letters[1:10], lag = 2, idx = c(1, 1, 1, 2, 3, 4, 6, 7, 8, 10), nearest = TRUE)
#> [1] NA "a" "a" "a" "a" "d" "f" "g" "g" "i"