slide2 {slider} | R Documentation |
Slide over multiple inputs simultaneously
Description
These are variants of slide()
that iterate over multiple inputs in
parallel. They are parallel in the sense that each input is processed in
parallel with the others, not in the sense of multicore computing. These
functions work similarly to map2()
and pmap()
from purrr.
Usage
slide2(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
slide2_vec(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.ptype = NULL
)
slide2_dbl(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
slide2_int(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
slide2_lgl(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
slide2_chr(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
slide2_dfr(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.names_to = rlang::zap(),
.name_repair = c("unique", "universal", "check_unique")
)
slide2_dfc(
.x,
.y,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.size = NULL,
.name_repair = c("unique", "universal", "check_unique", "minimal")
)
pslide(.l, .f, ..., .before = 0L, .after = 0L, .step = 1L, .complete = FALSE)
pslide_vec(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.ptype = NULL
)
pslide_dbl(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
pslide_int(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
pslide_lgl(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
pslide_chr(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE
)
pslide_dfr(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.names_to = rlang::zap(),
.name_repair = c("unique", "universal", "check_unique")
)
pslide_dfc(
.l,
.f,
...,
.before = 0L,
.after = 0L,
.step = 1L,
.complete = FALSE,
.size = NULL,
.name_repair = c("unique", "universal", "check_unique", "minimal")
)
Arguments
.x , .y |
Vectors to iterate over. Vectors of size 1 will be recycled. |
.f |
If a function, it is used as is. If a formula, e.g.
This syntax allows you to create very compact anonymous functions. |
... |
Additional arguments passed on to the mapped function. |
.before , .after |
The number of values before or after the current element to
include in the sliding window. Set to |
.step |
The number of elements to shift the window forward between function calls. |
.complete |
Should the function be evaluated on complete windows only? If |
.ptype |
A prototype corresponding to the type of the output. If If supplied, the result of each call to If |
.names_to |
This controls what to do with input names supplied in
|
.name_repair |
One of With |
.size |
If, Alternatively, specify the desired number of rows, and any inputs of length 1 will be recycled appropriately. |
.l |
A list of vectors. The length of |
Value
A vector fulfilling the following invariants:
slide2()
-
vec_size(slide2(.x, .y)) == vec_size_common(.x, .y)
-
vec_ptype(slide2(.x, .y)) == list()
slide2_vec()
and slide2_*()
variants
-
vec_size(slide2_vec(.x, .y)) == vec_size_common(.x, .y)
-
vec_size(slide2_vec(.x, .y)[[1]]) == 1L
-
vec_ptype(slide2_vec(.x, .y, .ptype = ptype)) == ptype
pslide()
-
vec_size(pslide(.l)) == vec_size_common(!!! .l)
-
vec_ptype(pslide(.l)) == list()
pslide_vec()
and pslide_*()
variants
-
vec_size(pslide_vec(.l)) == vec_size_common(!!! .l)
-
vec_size(pslide_vec(.l)[[1]]) == 1L
-
vec_ptype(pslide_vec(.l, .ptype = ptype)) == ptype
See Also
slide()
, slide_index2()
, hop_index2()
Examples
# Slide along two inputs at once
slide2(1:4, 5:8, ~list(.x, .y), .before = 2)
# Or, for more than two, use `pslide()`
pslide(list(1:4, 5:8, 9:12), ~list(.x, .y, ..3), .before = 2)
# You can even slide along the rows of multiple data frames of
# equal size at once
set.seed(16)
x <- data.frame(a = rnorm(5), b = rnorm(5))
y <- data.frame(c = letters[1:5], d = letters[6:10])
row_return <- function(x_rows, y_rows) {
if (sum(x_rows$a) < 0) {
x_rows
} else {
y_rows
}
}
slide2(x, y, row_return, .before = 1, .after = 2)