如何在 purrr 中使用像 reduce2 函数一样的累加? [英] How can I use accumulate like reduce2 function in purrr?
问题描述
我想使用带有两个输入向量的 accumulate
函数和 reduce2
函数.accumulate
的文档暗示可以给出两个输入向量,并且 accumulate
可以与 reduce2
一起使用.但是,我遇到了麻烦.
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.
这是一个示例,灵感来自 reduce2
的文档.
Here is an example, inspired by the documentation from reduce2
.
这是来自 reduce2
> paste2 <- function(x, y, sep = ".") paste(x, y, sep = sep)
> letters[1:4] %>% reduce2(.y=c("-", ".", "-"), paste2)
[1] "a-b.c-d"
这里有几次尝试使用 accumulate
类似于 reduce2
.没有一个能正确地遍历 letters[1:4]
和 c("-",".","-")
.
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"
我将如何使用 accumulate
来查看 reduce2
示例给出的中间结果?
How would I use accumulate
to see the intermediate results given by the reduce2
example?
推荐答案
这可能是一个疏忽,文档根本不是最新的/有点误导?我也无法让 accumulate
接受一个三参数函数,我很惊讶在你的最后一个例子中没有错误,尽管我猜它必须是 paste
抛出它..f
的文本与 accumulate
和 reduce
的文本完全相同,这一事实让我认为这不是功能存在于积累
.此外,查看源代码似乎表明(除非我误读)reduce
和 reduce2
有自己的实现,但 accumulate
依赖于 基础::减少
.可能值得一个 GitHub 问题.
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.
这是我制作您想要的输出的最佳方法.它基本上涉及使用输入列表的正确子集和对 paste2
的辅助输入向量多次调用 reduce2
,这感觉不是很整洁.这可能不是一个特别整洁的问题.请注意使用 {}
来覆盖将管道 LHS 作为第一个参数的默认 %>%
行为,以及 .x 上的不同索引
和 .y
在 reduce2
中(我们希望 .y
比 .x
短一个元素).
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
).
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"
由 reprex 包 (v0.2.0) 于 2018 年 5 月 11 日创建.
Created on 2018-05-11 by the reprex package (v0.2.0).
这篇关于如何在 purrr 中使用像 reduce2 函数一样的累加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!