嵌套的R foreach循环中的外循环变量 [英] Outer loop variable in nested R foreach loop

查看:270
本文介绍了嵌套的R foreach循环中的外循环变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在嵌套循环中使用foreach包,但是我的内部循环没有识别出外部的计数器,我错过了什么?

<$ p $ (100.05,0.5)
foreach(j = 2:length(v2))%:%{
foreach(i = 1:length(v3),.combine = rbind)%dopar%{
write.table(paste(v3 [i],paste(get.reactions.by.compound(v3 [i]),collapse = ),sep =),file1,quote = FALSE,row.names = FALSE,col.names = FALSE,append = TRUE)
write.table(paste(v3 [i] (get.pathways.by.compounds(v3 [i]),collapse =),sep =),file2,quote = FALSE,row.names = FALSE,col.names = FALSE,append = TRUE )
v3< - search.compounds.by.mass(v2 [j],0.5)
}
}
%:%操作符。它被设计为合并两个 foreach 对象,产生一个单独的 foreach 对象,可用于重复评估任何表达式你提供给它。所以,如果你想使用%:%,你需要第一合并两个 foreach()语句,然后然后使用结果对象驱动一个调用到%do%(或者在你的情况下,%dopar%)。以下(1)为例。另外,如果你想嵌套两个 foreach()对象,使用%做%两次,就像下面的(2)一样


无论哪种方式都行得通,更喜欢使用%:%的那个。你的代码虽然像下面的(3)那样,结合了两个策略的元素来产生一个混合,它不能做任何事情。



<$ p (1)或者合并两个或者两个以上的元素,并且将两个元素合并'foreach'对象使用'%:%'...
foreach(j = X,.combine = c)%:%foreach(i = Y,.combine = c)%do%{
粘贴(j,i,sep =)
}
#[1]A1A2A3B1B2B3


##(2)...或者使用一对'%do%'操作符嵌套两个'foreach'对象...
foreach(j = X,.combine = c)%do% {
foreach(i = Y,.combine = c)%do%{
paste(j,i,sep =)
}
}
# [1]A1A2A3B1B2B3


##(3)...但不要使用混合方法。
foreach(j = X,.combine = c)%:%{
foreach(i = Y,.combine = c)%do%{
paste(j,i,sep = )

$ b $ foreach(j = X,.combine = c)中的错误%:%{:
#%:%右操作数


I'm trying to use the foreach package in a nested loop, but my inner loop don't recognizes the outer's counter, what m I missing?

v3 <- search.compounds.by.mass(100.05,0.5)
foreach(j=2:length(v2)) %:% {
    foreach(i=1:length(v3), .combine=rbind) %dopar% {
        write.table(paste(v3[i], paste(get.reactions.by.compound(v3[i]), collapse=" "), sep=" "), "file1",quote=FALSE, row.names=FALSE, col.names=FALSE, append=TRUE)
        write.table(paste(v3[i], paste(get.pathways.by.compounds(v3[i]), collapse=" "), sep=" "), "file2",quote=FALSE, row.names=FALSE, col.names=FALSE, append=TRUE)
        v3 <- search.compounds.by.mass(v2[j],0.5)
    }
}

解决方案

The problem is that you are incorrectly applying the %:% operator. It is designed to merge two foreach objects, resulting in a single foreach object that can be used to repeatedly evaluate whatever expression you supply to it. So, if you want to use %:%, you need to first merge the two foreach() statements, and then use the resulting object to drive a single call to %do% (or in your case, %dopar%). See (1) below for an example.

Alternatively, if you want to nest the two foreach() objects, use %do% twice, as in (2) below.

Either way works, although for parallel jobs I might prefer the one using %:%. Your code, though, like (3) below, combines elements of the two strategies to produce a hybrid that can't do anything.

X <- c("A", "B")
Y <- 1:3

## (1) EITHER merge two 'foreach' objects using '%:%' ...
foreach (j = X, .combine = c) %:% foreach(i = Y, .combine = c) %do% {
    paste(j, i, sep = "")
}
# [1] "A1" "A2" "A3" "B1" "B2" "B3"


## (2) ... OR Nest two 'foreach' objects using a pair of '%do%' operators ...
foreach(j = X, .combine = c) %do% {
    foreach(i = Y, .combine = c) %do% {
        paste(j, i, sep = "")
    }
}
# [1] "A1" "A2" "A3" "B1" "B2" "B3"


## (3) ... BUT DON'T use a hybrid of the approaches.
foreach(j = X, .combine = c) %:% {
    foreach(i = Y, .combine = c) %do% {
        paste(j, i, sep = "")
    }
}
# Error in foreach(j = X, .combine = c) %:% { : 
#   "%:%" was passed an illegal right operand

这篇关于嵌套的R foreach循环中的外循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆