# The defaults work similarly to `map()`
slide(1:5, ~.x)
# Use `.before`, `.after`, and `.step` to control the window
slide(1:5, ~.x, .before = 1)
# This can be used for rolling means
slide_dbl(rnorm(5), mean, .before = 2)
# Or more flexible rolling operations
slide(rnorm(5), ~ .x - mean(.x), .before = 2)
# `.after` allows you to "align to the left" rather than the right
slide(1:5, ~.x, .after = 2)
# And a mixture of `.before` and `.after`
# allows you complete control over the exact alignment.
# Below, "center alignment" is used.
slide(1:5, ~.x, .before = 1, .after = 1)
# The `.step` controls how the window is shifted along `.x`,
# allowing you to "skip" iterations if you only need a less granular result
slide(1:10, ~.x, .before = 2, .step = 3)
# `.complete` controls whether or not partial results are computed.
# By default, they are, but setting `.complete = TRUE` restricts
# `slide()` to only evaluate the function where a complete window exists.
slide(1:5, ~.x, .before = 2, .after = 1)
slide(1:5, ~.x, .before = 2, .after = 1, .complete = TRUE)
# ---------------------------------------------------------------------------
# Data frames
# Data frames are iterated over rowwise
mtcars_rowwise <- slide(mtcars, ~.x)
mtcars_rowwise[1:3]
# This means that any column name is easily accessible
slide_dbl(mtcars, ~.x$mpg + .x$cyl)
# More advanced rowwise iteration is available as well by using the
# other arguments
mtcars_rowwise_window <- slide(mtcars, ~.x, .before = 1, .after = 1)
mtcars_rowwise_window[1:3]
# ---------------------------------------------------------------------------
# Cumulative sliding
# Using the special cased value, `Inf`, you can ask `slide()` to pin the
# start of the sliding window to the first element, effectively creating
# a cumulative window
slide(1:5, ~.x, .before = Inf)
# Same with `.after`, this creates a window where you start with all of the
# elements, but decrease the total number over each iteration
slide(1:5, ~.x, .after = Inf)
# ---------------------------------------------------------------------------
# Negative `.before` / `.after`
# `.before` is allowed to be negative, allowing you to "look forward" in
# your vector. Note that `abs(.before) <= .after` must hold if `.before` is
# negative. In this example, we look forward to elements in locations 2 and 3
# but place the result in position 1 in the output.
slide(1:5, ~.x, .before = -1, .after = 2)
# `.after` can be negative as well to "look backwards"
slide(1:5, ~.x, .before = 2, .after = -1)
# ---------------------------------------------------------------------------
# Removing padding
# If you are looking for a way to remove the `NA` values from something like
# this, then it doesn't exist as a built in option.
x <- rnorm(10)
slide_dbl(x, mean, .before = 3, .step = 2, .complete = TRUE)
# Adding an option to `slide_dbl()` to remove the `NA` values would destroy
# its size stability. Instead, you can use a combination of `slide_dfr()`
# to get the start/stop indices with `hop_index_vec()`.
i <- seq_along(x)
idx <- slide_dfr(
i,
~data.frame(start = .x[1], stop = .x[length(.x)]),
.before = 3,
.step = 2,
.complete = TRUE
)
idx
hop_index_vec(x, i, idx$start, idx$stop, mean, .ptype = double())
Run the code above in your browser using DataLab