[R] if(...){next} with "foreach" loop?

Greg Snow 538280 at gmail.com
Tue Feb 11 21:40:43 CET 2014


Since you may eventually do this in parallel (the reason for doing
foreach), it seems inefficient to use the overhead of sending a
proccess to a core only to have it skip any computations.  I think
instead I would create the combinations with `expand.grid` and then
remove the rows corresponding to the conditions that I want  to split
and then loop over the rows of the resulting data frame.  Possibly
something like

combs <- expand.grid( i= 1:10, j=1:2 )
combs <- combs[ ! ( (combs$i==2 & combs$j==1) | (combs$i=4 & combs$j==3)), ]
foreach( k= seq_len(nrow(combs)) %do% {

use combs$i[k] and combs$j[k] in the calculations

}





On Tue, Feb 11, 2014 at 3:30 AM, Marc Marí Dell'Olmo
<marceivissa at gmail.com> wrote:
> Dear all,
>
> I want to do a loop with foreach. But, I would like to include a condition
> with "next" to don't loop over some elements.
>
> For example, I would like to translate this "for loop" to a "foreach loop":
>
> for (i in 1:10) {
>   for (j in 1:2) {
>   ...
>   if((i==2 & j==1) | (i==4 & j==3)) { next }
>   ...
>   }
> }
>
> Foreach loop???
>
> foreach (i = c(1:10)) %do%{
>   foreach (j = c(1:2)) %do%{
>
> #I don't know how to include a "next"!!
> ...
>
>   }
> }
>
> Thank you!
>
> Marc
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com




More information about the R-help mailing list