reduce {purrr} | R Documentation |
reduce()
combines from the left, reduce_right()
combines from
the right. reduce(list(x1, x2, x3), f)
is equivalent to
f(f(x1, x2), x3)
; reduce_right(list(x1, x2, x3), f)
is equivalent to
f(f(x3, x2), x1)
.
reduce(.x, .f, ..., .init) reduce_right(.x, .f, ..., .init) reduce2(.x, .y, .f, ..., .init) reduce2_right(.x, .y, .f, ..., .init)
.x |
A list or atomic vector. |
.f |
For For |
... |
Additional arguments passed on to |
.init |
If supplied, will be used as the first value to start
the accumulation, rather than using |
.y |
For |
1:3 %>% reduce(`+`) 1:10 %>% reduce(`*`) paste2 <- function(x, y, sep = ".") paste(x, y, sep = sep) letters[1:4] %>% reduce(paste2) letters[1:4] %>% reduce2(c("-", ".", "-"), paste2) samples <- rerun(2, sample(10, 5)) samples reduce(samples, union) reduce(samples, intersect) x <- list(c(0, 1), c(2, 3), c(4, 5)) x %>% reduce(c) x %>% reduce_right(c) # Equivalent to: x %>% rev() %>% reduce(c)