[R] Looping and break

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Mar 3 03:04:46 CET 2015


Your example is decidedly not expressed in R, though it looks like you tried. Can you provide the hand-computed result that you are trying to obtain?

Note that the reason you cannot find anything about next or break in R is that they don't exist. There are generally alternative ways to accomplish the kinds of things you might want to accomplish without them, and those alternatives often don't involve explicit loops at all.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On March 2, 2015 4:11:21 PM PST, Scott Colwell <scolwell at uoguelph.ca> wrote:
>Hello,
>
>I apologies for bringing up next and break in loops given that there is
>so
>much on the net about it, but I've tried numerous examples found using
>Google and just can't seem to get this to work.
>
>This is a simple version of what I am doing with matrices but it shows
>the
>issue. I need to have the loop indexed as n to perform a calculation on
>the
>variable total. But if "total" is greater than 8, it goes to the next
>loop
>indexed "a".  For example, it does condition a = 1 for n = 1 to 50 but
>within n if total is greater than 8 it goes to the next condition of a
>which
>would be a = 2, and so on.
>
>for (a in 1:3){
>  
>  if (a == 1) { b <- c(1:5) }
>  if (a == 2) { b <- c(1:5) }
>  if (a == 3) { b <- c(1:5) }
>  
>  for (n in 1:50){
>  
>     if (n > 15) next
>    
>     total <- 2*b
>  
>     if (total > 8) next
>    
>  }
>}
>
>Any help would be greatly appreciated.
>
>Thanks,
>
>Scott
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Looping-and-break-tp4704093.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list