-1

I have a multi-nested for-loop that I need to restart the entire loop once the last nest (here, clip.groups) is complete. I have tried several options. Each layer involves rasters and I cannot vectorize it through apply, etc. Since there are so many input files, it is not reproducible.

The basic structure though is this:

clip.groups <- c('Bay area','Alameda County','Oakland','West and Downtown Oakland')
rate.groups <- c('co.25','cbg.25')
conc.groups <- c('ppb', 'ug')
pop.groups <- c('pop.ls.night.25')
beta.groups <- c(0.001105454,0.000318195,0.001881231)

for (j in 1:length(conc.groups)){
  for (i in 1:length(beta.groups)){
    for (k in 1:length(rate.groups)){
      for (h in 1:length(pop.groups)){
        for (m in 1:length(clip.groups)){
              break #==== THIS IS WHERE I NEED IT TO GO BACK TO THE OUTER MOST LOOP - (conc.groups j)  
            }
          }
        }
      }
    }
  }
}
vatsouth
  • 1
  • 1
  • Can you use a flag to break out of loops conditionally? See: https://stackoverflow.com/a/50475326/3460670 – Ben Sep 18 '19 at 00:12
  • Please give us an example of what you do in loop and how you need to break out to outer. Actual code helps illustrate your needs. Otherwise this reads as the [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) where you ask for help on your y solution (very nested `for` loop) but do not describe the x problem. – Parfait Sep 18 '19 at 03:31

1 Answers1

0

If you go back to the outermost loop than the inbetween loops are meaningless. That is you get this

clip.groups <- c('Bay area','Alameda County','Oakland','West and Downtown Oakland')
rate.groups <- c('co.25','cbg.25')
conc.groups <- c('ppb', 'ug')
pop.groups <- c('pop.ls.night.25')
beta.groups <- c(0.001105454,0.000318195,0.001881231)

for (j in 1:length(conc.groups)){
    beta.groups[1]
    rate.groups[1]
    pop.groups[1]
    for (m in 1:length(clip.groups)){
        cat(j, "-", m, "\n")
    }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63