tsibble (version 0.9.2)

slide: Sliding window calculation

Description

deprecated

Please consider using the slider package.

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.

Usage

slide(
  .x,
  .f,
  ...,
  .size = 1,
  .step = 1,
  .fill = NA,
  .partial = FALSE,
  .align = "right",
  .bind = FALSE
)

slide_dfr( .x, .f, ..., .size = 1, .step = 1, .fill = NA, .partial = FALSE, .align = "right", .bind = FALSE, .id = NULL )

slide_dfc( .x, .f, ..., .size = 1, .step = 1, .fill = NA, .partial = FALSE, .align = "right", .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).

.step

A positive integer for calculating at every specified step instead of every single step.

.fill

A value to fill at the left/center/right of the data range depending on .align (NA by default). NULL means no filling.

.partial

if TRUE, partial sliding.

.align

Align index at the "right", "centre"/"center", or "left" of the window. If .size is even for center alignment, "centre-right" & "centre-left" is needed.

.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.

Value

if .fill != NULL, it always returns the same length as input.

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.

slide() is intended to work with list (and column-wise data frame). To perform row-wise sliding window on data frame, please check out pslide().

  • .partial = TRUE allows for partial sliding. Window contains observations outside of the vector will be treated as value of .fill, which will be passed to .f.

  • .partial = FALSE restricts calculations to be done on complete sliding windows. Window contains observations outside of the vector will return the value .fill.

See Also

Other sliding window functions: slide2()

Examples

Run this code
# NOT RUN {
x <- 1:5
lst <- list(x = x, y = 6:10, z = 11:15)
slide_dbl(x, mean, .size = 2)
slide_dbl(x, mean, .size = 2, align = "center")
slide_lgl(x, ~ mean(.) > 2, .size = 2)
slide(lst, ~., .size = 2)
# }

Run the code above in your browser using DataCamp Workspace