I want to set a loop to run from 1 to 10. Then from within the loop I want to change the index so to skip iterations 6 and 7, and complete the loop with iterations 8, 9 and 10.
for (i in 1:10) {
print(i)
if (i == 5) {
i <- 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 my line 1 <- 8 is set by the function for to 6. Is there any way to prevent this?
Rallows to use a variable twice in this case and corrects for unusual programming style. The looping variable remains unaffected by the change ofiwithin the loop. The variable defined inside the loop is stored effectively as a separate variable.if(i satisifies condition){do something}construct?