下一个革命R的foreach包? [英] Next with Revolution R's foreach package?
问题描述
我已经浏览了大量的文档,并做了大量的Google搜索,但找不到以下问题的答案:是否有一种方法可以在并行<$ c中引入下一个功能$ c> foreach 使用 foreach
包的循环
喜欢做这样的事情(这不适用于下一步
但没有):
foreach(i = 1:10,.combine =c)%dopar%{
n < - i + floor(runif(1,0,9))
if n %% 3){next}
n
}
嵌套我的括号,但如果我想有一个长循环几个下一个条件,这很快就成为一个语法噩梦。
是否有一个简单的解决方法在这里类功能或不同的方法来解决这个问题)?解决方案您可以把你的代码放在一个函数中,并调用返回
。从你的例子中不清楚你想在 n %% 3
时做什么,所以我会返回 NA
。
funi < - function(i){
n < - i + floor(runif(1,0, 9))
if(n %% 3)return(NA)
n
}
foreach(i = 1:10,.combine =c)%dopar%{ funi(i)}
I've looked through much of the documentation and done a fair amount of Googling, but can't find an answer to the following question: Is there a way to induce 'next-like' functionality in a parallel foreach
loop using the foreach
package?
Specifically, I'd like to do something like (this doesn't work with next
but does without):
foreach(i = 1:10, .combine = "c") %dopar% {
n <- i + floor(runif(1, 0, 9))
if (n %% 3) {next}
n
}
I realize I can nest my brackets, but if I want to have a few next conditions over a long loop this very quickly becomes a syntax nightmare.
Is there an easy workaround here (either next-like functionality or a different way of approaching the problem)?
You could put your code in a function and call return
. It's not clear from your example what you want it to do when n %% 3
so I'll return NA
.
funi <- function(i) {
n <- i + floor(runif(1, 0, 9))
if (n %% 3) return(NA)
n
}
foreach(i = 1:10, .combine = "c") %dopar% { funi(i) }
这篇关于下一个革命R的foreach包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!