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.
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 at yahoo.com>
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 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
? `break` On Tue, Jan 6, 2009 at 7:58 PM, kayj <kjaja27@yahoo.com> wrote:> I was wondering if there is anything that breaks a loop in R.... >[[alternative HTML version deleted]]
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
--
View this message in context:
http://www.nabble.com/breaking-a-loop-in-R-tp21322868p24325853.html
Sent from the R help mailing list archive at Nabble.com.