Learn R Programming

runner (version 0.3.0)

which_run: Running which

Description

min_run calculates running which - returns index of element where x == TRUE.

Usage

which_run(x, k = integer(1), lag = integer(1), which = "last",
  na_rm = TRUE, na_pad = FALSE, idx = integer(0))

Arguments

x

Vector of any type

k

integer vector or single value denoting size of the running window. If k is a single value then window size is constant for all elements, otherwise if length(k) == length(x) different window size for each element.

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.

which

character value "first" or "last" denoting if the first or last TRUE index is returned from the window.

na_rm

logical single value (default na_rm = TRUE) - if TRUE sum is calculating excluding NA.

na_pad

logical single value (default na_pad=FALSE) - if TRUE calculation on incomplete window will return NA. Incomplete window is when some parts of the window are out of range

idx

date or integer an optional integer vector containing index of observation. If specified then k and lag are depending on idx. Length of idx should be equal of length x

Value

integer vector of indexes of the same length as x.

Examples

Run this code
# NOT RUN {
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)
which_run(x2, na_rm = TRUE)
which_run(x2, na_rm = TRUE, k = 4)
which_run(x2, na_rm = FALSE, k = k)
# }

Run the code above in your browser using DataLab