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
Post a Comment