[R] Looping and break

Scott Colwell scolwell at uoguelph.ca
Tue Mar 3 01:11:21 CET 2015


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.



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.

More information about the R-help mailing list