min_run calculates running which - returns index of element where x == TRUE.
(vector, data.frame, matrix, xts, grouped_df)
input data.
(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.
(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.
(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.
(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.
character value "first" or "last" denoting if the first or last TRUE
index is returned from the window.
logical single value (default na_rm = TRUE) -
if TRUE sum is calculating excluding NA.
(logical)
If TRUE, return NA for windows that extend beyond the data range.
integer vector of indexes of the same length as x.
set.seed(11)
x1 <- sample(c(1, 2, 3), 15, replace = TRUE)
x2 <- sample(c(NA, 1, 2, 3), 15, replace = TRUE)
k <- sample(1:4, 15, replace = TRUE)
which_run(x1)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
which_run(x2, na_rm = TRUE)
#> [1] 1 2 2 4 4 6 7 8 9 10 11 12 13 14 15
which_run(x2, na_rm = TRUE, k = 4)
#> [1] 1 2 2 4 4 6 7 8 9 10 11 12 13 14 15
which_run(x2, na_rm = FALSE, k = k)
#> [1] 1 2 NA 4 NA 6 7 8 9 10 11 12 13 14 15