这可能是/可能不是
How can I use accumulate like reduce2 function in purrr?
我在努力理解
purrr::accummulate
.x-列表或原子向量。
函数将作为第一个传递累积值
参数和“下一个”值作为第二个参数。对于reduce2(),a
三元函数。函数将传递累积值
以及
作为第三个参数的.y的下一个值
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"
我遗漏了什么吗?