Hi, Are there labeled break statements in R? i.e., something along the lines of TOPLOOP: for(i in 1:m) { for(j in 1:n) { ... if(condition) { break TOPLOOP } } } Thanks, Roger
On 18 Aug 2004, Roger Levy wrote:> Are there labeled break statements in R? i.e., something along the > lines of > > TOPLOOP: for(i in 1:m) { > for(j in 1:n) { > ... > if(condition) { > break TOPLOOP > } > } > }No, but if you find yourself using nested for loops it is very likely that you are not thinking in the right way for a vector language. R does have a `R Language Definition' manual. It's long been an unfinished draft but it is not so incomplete as to omit reserved words. It and the S reference books are the places to research questions like this. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Roger Levy <rog <at> stanford.edu> writes: : : Hi, : : Are there labeled break statements in R? i.e., something along the : lines of : : TOPLOOP: for(i in 1:m) { : for(j in 1:n) { : ... : if(condition) { : break TOPLOOP : } : } : } Assuming that your labelled break is supposed to break out of both loops, unlike an ordinary break that just breaks out of the inner loop, this is how it would be done: a <- matrix(0, nr=3, nc=3) local({ for(i in 1:3) for(j in 1:3) if (i+j>4) return() else a[i,j] <<- i+j }) a Be aware that assigning variables within the local will assign local copies unless you use <<-, assign(..., ..., parent.frame()) or eval.parent(...). Of course this style is not recommended for R, and as someone else already mentioned, you should try to vectorize your code eliminating the loops altogether. For example, the above could be written without loops like this: a <- matrix(0, nr=3, nc=3) rc <- row(a) + col(a) a <- ifelse(rc > 4, a, rc) a
Gabor Grothendieck <ggrothendieck <at> myway.com> writes: : : Roger Levy <rog <at> stanford.edu> writes: : : : : : Hi, : : : : Are there labeled break statements in R? i.e., something along the : : lines of : : : : TOPLOOP: for(i in 1:m) { : : for(j in 1:n) { : : ... : : if(condition) { : : break TOPLOOP : : } : : } : : } : : Assuming that your labelled break is supposed to break out of : both loops, unlike an ordinary break that just breaks out of : the inner loop, this is how it would be done: : : a <- matrix(0, nr=3, nc=3) : local({ : for(i in 1:3) : for(j in 1:3) : if (i+j>4) return() else a[i,j] <<- i+j : }) : a : : Be aware that assigning variables within the local will assign : local copies unless you use <<-, assign(..., ..., parent.frame()) : or eval.parent(...). : : Of course this style is not recommended for R, and as someone else : already mentioned, you should try to vectorize your code eliminating : the loops altogether. For example, the above could be written : without loops like this: : : a <- matrix(0, nr=3, nc=3) : rc <- row(a) + col(a) : a <- ifelse(rc > 4, a, rc) : a : Sorry, the ifelse line should have been: ifelse( t(matrix(cumsum(rc>4),3,3)), a, rc)
Gabor Grothendieck <ggrothendieck <at> myway.com> writes: : : Gabor Grothendieck <ggrothendieck <at> myway.com> writes: : : : : : Roger Levy <rog <at> stanford.edu> writes: : : : : : : : : Hi, : : : : : : Are there labeled break statements in R? i.e., something along the : : : lines of : : : : : : TOPLOOP: for(i in 1:m) { : : : for(j in 1:n) { : : : ... : : : if(condition) { : : : break TOPLOOP : : : } : : : } : : : } : : : : Assuming that your labelled break is supposed to break out of : : both loops, unlike an ordinary break that just breaks out of : : the inner loop, this is how it would be done: : : : : a <- matrix(0, nr=3, nc=3) : : local({ : : for(i in 1:3) : : for(j in 1:3) : : if (i+j>4) return() else a[i,j] <<- i+j : : }) : : a : : : : Be aware that assigning variables within the local will assign : : local copies unless you use <<-, assign(..., ..., parent.frame()) : : or eval.parent(...). : : : : Of course this style is not recommended for R, and as someone else : : already mentioned, you should try to vectorize your code eliminating : : the loops altogether. For example, the above could be written : : without loops like this: : : : : a <- matrix(0, nr=3, nc=3) : : rc <- row(a) + col(a) : : a <- ifelse(rc > 4, a, rc) : : a : : : Sorry, the ifelse line should have been: : : ifelse( t(matrix(cumsum(rc>4),3,3)), a, rc) or even simpler: a <- ifelse( matrix(cumsum(rc>4),3,byrow=T), a, rc)
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:> Roger Levy <rog <at> stanford.edu> writes: > : Are there labeled break statements in R? i.e., something along the > : lines of > : > : TOPLOOP: for(i in 1:m) { > : for(j in 1:n) { > : ... > : if(condition) { > : break TOPLOOP > : } > : } > : } > > Assuming that your labelled break is supposed to break out of > both loops, unlike an ordinary break that just breaks out of > the inner loop, this is how it would be done: > > a <- matrix(0, nr=3, nc=3) > local({ > for(i in 1:3) > for(j in 1:3) > if (i+j>4) return() else a[i,j] <<- i+j > }) > a > > Be aware that assigning variables within the local will assign > local copies unless you use <<-, assign(..., ..., parent.frame()) > or eval.parent(...).An improvement over the above is to use evalq as this does not set up a local environment, thereby avoiding the problems cited in the last sentence: a <- matrix(0, nr=3, nc=3) evalq({ for(i in 1:3) for(j in 1:3) if (i+j>4) return() else a[i,j] <- i+j }) a Note that we were able to replace the <<- with <-.