Set window parameters for runner(). This function sets the attributes to x (only data.frame) object and saves user effort to specify window parameters in further multiple runner() calls.

run_by(x, idx, k, lag, na_pad, at)

Arguments

x

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

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.

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.

na_pad

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

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.

Value

x object which runner() can be executed on.

Examples

if (FALSE) { # \dontrun{
library(dplyr)

data <- data.frame(
  index = c(2, 3, 3, 4, 5, 8, 10, 10, 13, 15),
  a = rep(c("a", "b"), each = 5),
  b = 1:10
)

data %>%
  group_by(a) %>%
  run_by(idx = "index", k = 5) %>%
  mutate(
    c = runner(
      x = .,
      f = function(x) {
        paste(x$b, collapse = ">")
      }
    ),
    d = runner(
      x = .,
      f = function(x) {
        sum(x$b)
      }
    )
  )
} # }