breaking a loop in R

View: New views
5 Messages — Rating Filter:   Alert me  

breaking a loop in R

by kayj :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

I was wondering if there is anything that breaks a loop in R. For example,


For (k in 1:100)
For ( i in 1:10){

If ( condition ){
Break the inner loop
}

}
}

In the above case, if the program runs the if statement, then I want the inner loop for (i in 1:10) to stop looping and skip the rest of the program.

Thanks for your help


Re: breaking a loop in R

by jholtman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

something like this should do it:

breakFlag <- FALSE
For (k in 1:100)
    For ( i in 1:10){

        If ( condition ){
            breakFlag <- TRUE
            break
        }

    }
    if (breakFlag) break
}

On Tue, Jan 6, 2009 at 7:58 PM, kayj <kjaja27@...> wrote:

>
> Hi All,
>
> I was wondering if there is anything that breaks a loop in R. For example,
>
>
> For (k in 1:100)
> For ( i in 1:10){
>
> If ( condition ){
> Break the inner loop
> }
>
> }
> }
>
> In the above case, if the program runs the if statement, then I want the
> inner loop for (i in 1:10) to stop looping and skip the rest of the program.
>
> Thanks for your help
>
>
> --
> View this message in context: http://www.nabble.com/breaking-a-loop-in-R-tp21322868p21322868.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@... 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
R-help@... 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.

Re: breaking a loop in R

by Stavros Macrakis-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

? `break`

On Tue, Jan 6, 2009 at 7:58 PM, kayj <kjaja27@...> wrote:

> I was wondering if there is anything that breaks a loop in R....
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.

Re: breaking a loop in R

by NatsS :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Please could someone explain

break and next??

Is there a way to break a loop that is not the innermost loop??

for example:
#=========================
dim A = 1000 x 3
dim B = 2000 x 3

for (a in 1:1000){
    for (b in 1:2000){
         expr = intersect(A[a,1]:A[a,2],B[b,1]:B[b,2])
         if (!is.na(expr[1])){
             indx = which(expr[1] >= A[,2])
             if (length(indx) > 1){
                for(c in 1:length(indx))
                     X[a,1] = paste(.....)
                     .....
             }else {
                 X[a,1] = A[indx, 3]
             }
          }            
          ###BREAK - Here does not work
                NEXT # am not sure if this is doing what I want it to??
      }
}      
#=============================

Any help and advice would be much appreciated!

Many Thanks,
NS                

Re: breaking a loop in R

by Allan Engelhardt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The break command can only break the innermost loop (the b loop in your
case).  Rewrite the loop control logic.  Here is one example

end.a <- FALSE;
for (a in 1:10) {
   print(a);
   for (b in 1:20) {
     if (a > 5 && b > 5 ) {
       end.a <- TRUE;
       break; # Breaks the b loop
     }
   };
   if ( end.a ) break;
}

(though I prefer rewriting as while statements once the logic gets
complex like this).

There is, AFAIK, no equivalent of (say) perl's "last LABEL"
functionality.  (I miss perl's rich contunue/last/next/redo functionality.)

Hope this helps a little.

Allan



On 03/07/09 17:45, NatsS wrote:

> Hello,
>
> Please could someone explain
>
> break and next??
>
> Is there a way to break a loop that is not the innermost loop??
>
> for example:
> #=========================
> dim A = 1000 x 3
> dim B = 2000 x 3
>
> for (a in 1:1000){
>      for (b in 1:2000){
>           expr = intersect(A[a,1]:A[a,2],B[b,1]:B[b,2])
>           if (!is.na(expr[1])){
>               indx = which(expr[1]>= A[,2])
>               if (length(indx)>  1){
>                  for(c in 1:length(indx))
>                       X[a,1] = paste(.....)
>                       .....
>               }else {
>                   X[a,1] = A[indx, 3]
>               }
>            }
>            ###BREAK - Here does not work
>                  NEXT # am not sure if this is doing what I want it to??
>        }
> }
> #=============================
>
> Any help and advice would be much appreciated!
>
> Many Thanks,
> NS
>    

        [[alternative HTML version deleted]]

______________________________________________
R-help@... 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.