Description Usage Arguments Value Life cycle Direction See Also Examples
accumulate()
sequentially applies a 2argument function to elements of a
vector. Each application of the function uses the initial value or result
of the previous application as the first argument. The second argument is
the next value of the vector. The results of each application are
returned in a list. The accumulation can optionally terminate before
processing the whole vector in response to a done()
signal returned by
the accumulation function.
By contrast to accumulate()
, reduce()
applies a 2argument function in
the same way, but discards all results except that of the final function
application.
accumulate2()
sequentially applies a function to elements of two lists, .x
and .y
.
1 2 3  accumulate(.x, .f, ..., .init, .dir = c("forward", "backward"))
accumulate2(.x, .y, .f, ..., .init)

.x 
A list or atomic vector. 
.f 
For For The accumulation terminates early if 
... 
Additional arguments passed on to the mapped function. 
.init 
If supplied, will be used as the first value to start
the accumulation, rather than using 
.dir 
The direction of accumulation as a string, one of

.y 
For 
A vector the same length of .x
with the same names as .x
.
If .init
is supplied, the length is extended by 1. If .x
has
names, the initial value is given the name ".init"
, otherwise
the returned vector is kept unnamed.
If .dir
is "forward"
(the default), the first element is the
initial value (.init
if supplied, or the first element of .x
)
and the last element is the final reduced value. In case of a
right accumulation, this order is reversed.
The accumulation terminates early if .f
returns a value wrapped
in a done()
. If the done box is empty, the last value is
used instead and the result is one element shorter (but always
includes the initial value, even when terminating at the first
iteration).
accumulate_right()
is softdeprecated in favour of the .dir
argument as of rlang 0.3.0. Note that the algorithm has
slightly changed: the accumulated value is passed to the right
rather than the left, which is consistent with a right reduction.
When .f
is an associative operation like +
or c()
, the
direction of reduction does not matter. For instance, reducing the
vector 1:3
with the binary function +
computes the sum ((1 + 2) + 3)
from the left, and the same sum (1 + (2 + 3))
from the
right.
In other cases, the direction has important consequences on the
reduced value. For instance, reducing a vector with list()
from
the left produces a leftleaning nested list (or tree), while
reducing list()
from the right produces a rightleaning list.
reduce()
when you only need the final reduced value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74  # With an associative operation, the final value is always the
# same, no matter the direction. You'll find it in the last element for a
# backward (left) accumulation, and in the first element for forward
# (right) one:
1:5 %>% accumulate(`+`)
1:5 %>% accumulate(`+`, .dir = "backward")
# The final value is always equal to the equivalent reduction:
1:5 %>% reduce(`+`)
# It is easier to understand the details of the reduction with
# `paste()`.
accumulate(letters[1:5], paste, sep = ".")
# Note how the intermediary reduced values are passed to the left
# with a left reduction, and to the right otherwise:
accumulate(letters[1:5], paste, sep = ".", .dir = "backward")
# `accumulate2()` is a version of `accumulate()` that works with
# 3argument functions and one additional vector:
paste2 < function(x, y, sep = ".") paste(x, y, sep = sep)
letters[1:4] %>% accumulate(paste2)
letters[1:4] %>% accumulate2(c("", ".", ""), paste2)
# You can shortcircuit an accumulation and terminate it early by
# returning a value wrapped in a done(). In the following example
# we return early if the resultsofar, which is passed on the LHS,
# meets a condition:
paste3 < function(out, input, sep = ".") {
if (nchar(out) > 4) {
return(done(out))
}
paste(out, input, sep = sep)
}
letters %>% accumulate(paste3)
# Note how we get twice the same value in the accumulation. That's
# because we have returned it twice. To prevent this, return an empty
# done box to signal to accumulate() that it should terminate with the
# value of the last iteration:
paste3 < function(out, input, sep = ".") {
if (nchar(out) > 4) {
return(done())
}
paste(out, input, sep = sep)
}
letters %>% accumulate(paste3)
# Here the early return branch checks the incoming inputs passed on
# the RHS:
paste4 < function(out, input, sep = ".") {
if (input == "f") {
return(done())
}
paste(out, input, sep = sep)
}
letters %>% accumulate(paste4)
# Simulating stochastic processes with drift
## Not run:
library(dplyr)
library(ggplot2)
rerun(5, rnorm(100)) %>%
set_names(paste0("sim", 1:5)) %>%
map(~ accumulate(., ~ .05 + .x + .y)) %>%
map_dfr(~ tibble(value = .x, step = 1:100), .id = "simulation") %>%
ggplot(aes(x = step, y = value)) +
geom_line(aes(color = simulation)) +
ggtitle("Simulations of a random walk with drift")
## End(Not run)

[1] 1 3 6 10 15
[1] 15 14 12 9 5
[1] 15
[1] "a" "a.b" "a.b.c" "a.b.c.d" "a.b.c.d.e"
[1] "a.b.c.d.e" "b.c.d.e" "c.d.e" "d.e" "e"
[1] "a" "a.b" "a.b.c" "a.b.c.d"
[[1]]
[1] "a"
[[2]]
[1] "ab"
[[3]]
[1] "ab.c"
[[4]]
[1] "ab.cd"
[1] "a" "a.b" "a.b.c" "a.b.c"
[1] "a" "a.b" "a.b.c"
[1] "a" "a.b" "a.b.c" "a.b.c.d" "a.b.c.d.e"
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.