R: Set loop index from within the loop -


i want set loop run 1 10. within loop want change index skip iterations 6 , 7, , complete loop iterations 8, 9 , 10.

for (i in 1:10) {   print(i)    if (i == 5) {     <- 8     print(i)   } } [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 5 [1] 8 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 

clearly, i after line 1 <- 8 set function for 6. there way prevent this?

as you're talking skipping, best idea use next on values wish skip:

for (i in 1:10) {   if (i %in% c(6,7)) {     next   }   print(i)  } 

quote help("for"):

next halts processing of current iteration , advances looping index.

another option restrict ranges looping on this:

for(i in c(1:5,8:10)) {   print(i) } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -