How can I use accumulate like reduce2 function in purrr?


I would like to use the accumulate function with two input vectors and the reduce2 function. The documentation for accumulate implies that two input vectors can be given and that accumulate can work with reduce2. However, I am having trouble.

Here is an example, inspired by the documentation from reduce2.

This is the example from reduce2

> paste2 <- function(x, y, sep = ".") paste(x, y, sep = sep) > letters[1:4] %>% reduce2(.y=c("-", ".", "-"), paste2) [1] "a-b.c-d"

Here are several attempts to use accumulate similarly to reduce2. None properly iterate through both letters[1:4] and c("-",".","-").

> letters[1:4] %>% accumulate(.y=c("-", ".", "-"),paste2) Error in .f(x, y, ...) : unused argument (.y = c("-", ".", "-")) > letters[1:4] %>% accumulate(c("-", ".", "-"),paste2) [[1]] [1] "a" [[2]] NULL > letters[1:4] %>% accumulate(sep=c("-", ".", "-"),paste2) [1] "a" "a-b" "a-b-c" "a-b-c-d"

How would I use accumulate to see the intermediate results given by the reduce2 example?


It is possible that this is an oversight where the documentation is simply not up to date/a bit misleading? I could not get accumulate to accept a three argument function either, and I'm surprised there's no error in your last example though I guess it would have to be paste that throws it. The fact that the text for .f is exactly the same for accumulate as for reduce makes me think that this just isn't functionality present in accumulate. Additionally, looking at the source seems to show (unless I misread) that reduce and reduce2 have their own implementation but accumulate relies on base::Reduce. Might be worth a GitHub issue.

Here's my best shot at producing the output you wanted. It basically involves calling reduce2 multiple times with the right subset of the input list and the secondary input vector to paste2, which doesn't feel very neat or tidy. This might just not be a particularly neat or tidy problem. Note the use of the {} to override the default %>% behaviour of placing the pipe LHS as the first argument, and the different indexing on .x and .y inside reduce2 (we want to keep .y one element shorter than .x).

<pre class="lang-r prettyprint-override">paste2 <- function(x, y, sep = ".") paste(x, y, sep = sep) library(purrr) letters[1:4] %>% {map_chr( .x = 2:length(.), .f = function(index) reduce2( .x = .[1:index], .y = c("-", ".", "-")[1:(index - 1)], .f = paste2 ) )} #> [1] "a-b" "a-b.c" "a-b.c-d"

Created on 2018-05-11 by the <a href="http://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.2.0).


  • Vaadin Upload Button, CSS to change its color (same styles as Button)?
  • mysql query taking 46 seconds - needs to be more efficient
  • How to use the cursor in the new C++ Mongo Driver
  • local push notification badge number
  • Typescript - How to check if a given value is in a union type array
  • Does CFHostGetAddressing() support ipv6 DNS entries?
  • Proper (tree) display of Qt project using .pro and .pri in QtCreator
  • IllegalArgumentException: the error message does not make sense
  • Do lifetime parameters in `*(&'a mut self)` methods confuse the BorrowChecker?
  • displaying # views on a page without hitting database all the time
  • interpolation in 3d computer graphics
  • How to properly add strings to QListWidgets?
  • How many percent of the tweets does twitter sample API give?
  • C++ accessing vector
  • Yii2: Finding file and getting path in a directory tree
  • joining two bezier curves
  • Clear activity stack before launching another activity
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • How do I configure context broker accept post requests from my remote sensor?
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • d3 v4 drag and drop with TypeScript
  • Allowing both email and username for authentication
  • Why does access(2) check for real and not effective UID?
  • Read a local file using javascript
  • Get one-time binding to work for ng-if
  • Illegal mix of collations for operation for date/time comparison
  • Javascript Callbacks with Object constructor
  • How can I use Kendo UI with Razor?
  • Release, debug version and Authorization Google?
  • How do you troubleshoot character encoding problems?
  • R: gsub and capture
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • LevelDB C iterator
  • Django query for large number of relationships
  • How can i traverse a binary tree from right to left in java?