Fixing an initial window and expanding more observations:

  • stretch() always returns a list.

  • stretch_lgl(), stretch_int(), stretch_dbl(), stretch_chr() use the same arguments as stretch(), but return vectors of the corresponding type.

  • stretch_dfr() stretch_dfc() return data frames using row-binding & column-binding.

stretch(.x, .f, ..., .size = 1, .init = 1, .bind = FALSE)

stretch_dfr(.x, .f, ..., .size = 1, .init = 1, .bind = FALSE,
  .id = NULL)

stretch_dfc(.x, .f, ..., .size = 1, .init = 1, .bind = FALSE)

Arguments

.x

An object to slide over.

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to the mapped function.

.size

An integer for window size. If positive, moving forward from left to right; if negative, moving backward (from right to left).

.init

A positive integer for an initial window size.

.bind

If .x is a list, should .x be combined before applying .f? If .x is a list of data frames, row binding is carried out.

.id

Either a string or NULL. If a string, the output will contain a variable with that name, storing either the name (if .x is named) or the index (if .x is unnamed) of the input. If NULL, the default, no variable will be created.

Only applies to _dfr variant.

See also

  • future_stretch for stretching window in parallel

  • slide for sliding window with overlapping observations

  • tile for tiling window without overlapping observations

Other stretching window functions: stretch2

Examples

x <- 1:5 lst <- list(x = x, y = 6:10, z = 11:15) stretch_dbl(x, mean, .size = 2)
#> [1] 1 2 3
stretch_lgl(x, ~ mean(.) > 2, .size = 2)
#> [1] FALSE FALSE TRUE
stretch(lst, ~ ., .size = 2)
#> [[1]] #> [[1]]$x #> [1] 1 2 3 4 5 #> #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> [[2]]$z #> [1] 11 12 13 14 15 #> #>