【发布时间】:2015-01-29 06:54:54
【问题描述】:
我正在阅读 r 中 foreach 包的文档,并且能够理解 .combine 是如何在 c、rbind 和 cbind 上完成的。但是文档说,如果您想使用此函数进行组合,您也可以将函数的名称传递给语句中的.combine。尝试访问我正在组合的此函数中每个 foreach 的各个元素时,我无法理解输出。 (让我把这个函数称为mycombinefunc)
这是我尝试过的。我不明白为什么在 foreach 语句的输出中会打印两次输出。
> mycombinefunc= function(...){mylist = list(...);print(mylist)}
> foreach(i=1:2, .combine='mycombinefunc') %do% {c(1:5)}
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] 1 2 3 4 5
[[1]]
[1] 1 2 3 4 5
[[2]]
[1] 1 2 3 4 5
#just calling the function directly to show what the expected output for one iteration of the foreach loop looks like. I would have expected that the above function call would produce two copies of the below output
> mycombinefunc(c(1:5))
[[1]]
[1] 1 2 3 4 5
【问题讨论】:
标签: r parallel-foreach