Creates list of windows with given arguments settings. Length of output list is equal

window_run(
  x,
  k = integer(0),
  lag = integer(1),
  idx = integer(0),
  at = integer(0),
  na_pad = FALSE
)

Arguments

x

(vector, data.frame, matrix, xts, grouped_df)
input data.

k

(integer or character)
Window size. Single value or vector of length(x). Omit for cumulative windows. Accepts time-interval strings (e.g. "5 days") when idx is set.

lag

(integer or character)
Window shift. Positive shifts back, negative shifts forward. Single value or vector of length(x). Accepts time-interval strings when idx is set.

idx

(integer, Date, POSIXt)
Sorted index of observations. When set, k and lag refer to index distance rather than element count. Must be same length as x.

at

(integer, Date, POSIXt, character)
Indices at which to evaluate windows. Output length equals length(at) instead of length(x). A single time-interval string (e.g. "month") generates a regular sequence over the range of idx.

na_pad

(logical)
If TRUE, return NA for windows that extend beyond the data range.

Value

list of vectors (windows). Length of list is the same as length(x) or length(at) if specified, and length of each window is defined by k (unless window is out of range).

Examples

window_run(1:10, k = 3, lag = -1)
#> [[1]]
#> [1] 1 2
#> 
#> [[2]]
#> [1] 1 2 3
#> 
#> [[3]]
#> [1] 2 3 4
#> 
#> [[4]]
#> [1] 3 4 5
#> 
#> [[5]]
#> [1] 4 5 6
#> 
#> [[6]]
#> [1] 5 6 7
#> 
#> [[7]]
#> [1] 6 7 8
#> 
#> [[8]]
#> [1] 7 8 9
#> 
#> [[9]]
#> [1]  8  9 10
#> 
#> [[10]]
#> [1]  9 10
#> 
window_run(letters[1:10], k = c(1, 2, 2, 4, 5, 5, 5, 5, 5, 5))
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [1] "a" "b"
#> 
#> [[3]]
#> [1] "b" "c"
#> 
#> [[4]]
#> [1] "a" "b" "c" "d"
#> 
#> [[5]]
#> [1] "a" "b" "c" "d" "e"
#> 
#> [[6]]
#> [1] "b" "c" "d" "e" "f"
#> 
#> [[7]]
#> [1] "c" "d" "e" "f" "g"
#> 
#> [[8]]
#> [1] "d" "e" "f" "g" "h"
#> 
#> [[9]]
#> [1] "e" "f" "g" "h" "i"
#> 
#> [[10]]
#> [1] "f" "g" "h" "i" "j"
#>