0

This may/may not be a duplicate of How can I use accumulate like reduce2 function in purrr? but I couldn't really understand the case being discussed there so asking this again.

I am trying to understand the working of purrr::accummulate especially when passing 3 arguments. Here's documentation for it -

.x - A list or atomic vector.

.f - For reduce(), a 2-argument function. The function will be passed the accumulated value as the first argument and the "next" value as the second argument. For reduce2(), a 3-argument function. The function will be passed the accumulated value as the first argument, the next value of .x as the second argument, and the next value of .y as the third argument.

Based on above documentation -

library(purrr)

# 2-argument use is pretty straightforward
accumulate(.x = 1:3, .f = sum)
[1] 1 3 6 # 1, 1+2, 1+2+3

# 3-argument result seems weird
accumulate(.x = 1:3, .y = 1:2, .f = sum)
[1]  1  6 12 # seems like 1, 1+2+3, 1+2+3+3+3
# expecting 1 4 9 i.e. 1, 1+2+1, 1+2+1+3+2

# reduce2 works correctly and gives 9
reduce2(.x = 1:3, .y = 1:2, .f = sum)
[1] 9

# it seems to take sum(y) as third argument instead of "next value of .y"

Am I missing something?

Shree
  • 10,835
  • 1
  • 14
  • 36
  • That description is for `reduce2`, but there is no `accumulate2()` function. There is no three argument version of `acculultate()`. Those `.y` values are just being passed though the `...` in the function signature. What is the output you expect? – MrFlick Oct 16 '18 at 16:22
  • 1
    It's possible that answer is right about the documentation being misleading. Why is it in there if it doesn't apply? It reads like it's suggestive of being used within `.f`. Another [SO](https://stackoverflow.com/questions/52130992/cumulative-multiplication-in-r-with-the-addition-of-extra-constants) answer lists it as an [open issue from June 2018](https://github.com/tidyverse/purrr/issues/511). – Anonymous coward Oct 16 '18 at 16:27
  • @MrFlick...I have added expected output and output for `reduce2`. I think your comment makes sense although in that case the documentation for `accumulate` is certainly wrong. – Shree Oct 16 '18 at 17:24
  • @Anonymouscoward Thanks for the info. I think it all makes sense now. – Shree Oct 16 '18 at 17:28

0 Answers0