Rolling window with overlapping observations:

  • slide() always returns a list.

  • slide_lgl(), slide_int(), slide_dbl(), slide_chr() use the same arguments as slide(), but return vectors of the corresponding type.

  • slide_dfr() slide_dfc() return data frames using row-binding & column-binding.

slide(.x, .f, ..., .size = 1, .fill = NA, .partial = FALSE)

slide_dfr(.x, .f, ..., .size = 1, .fill = NA, .partial = FALSE,
  .id = NULL)

slide_dfc(.x, .f, ..., .size = 1, .fill = NA, .partial = FALSE)

Arguments

.x

An object to slide over.

.f

A function, formula, or atomic vector.

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. Within a list, wrap strings in get-attr() to extract named attributes. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to .f.

.size

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

.fill

A single value or data frame to replace NA.

.partial

if TRUE, partial sliding.

.id

If not NULL a variable with this name will be created giving either the name or the index of the data frame.

Details

The slide() function attempts to tackle more general problems using the purrr-like syntax. For some specialist functions like mean and sum, you may like to check out for RcppRoll for faster performance.

See also

  • slide2, pslide

  • tile for tiling window without overlapping observations

  • stretch for expanding more observations

Examples

.x <- 1:5 .lst <- list(x = .x, y = 6:10, z = 11:15) slide_dbl(.x, mean, .size = 2)
#> [1] NA 1.5 2.5 3.5 4.5
slide_lgl(.x, ~ mean(.) > 2, .size = 2)
#> [1] NA FALSE TRUE TRUE TRUE
slide(.lst, ~ ., .size = 2)
#> [[1]] #> [1] NA #> #> [[2]] #> [[2]]$x #> [1] 1 2 3 4 5 #> #> [[2]]$y #> [1] 6 7 8 9 10 #> #> #> [[3]] #> [[3]]$y #> [1] 6 7 8 9 10 #> #> [[3]]$z #> [1] 11 12 13 14 15 #> #>