In general, next
tells a for-loop to skip TO the next iteration. I dont think there is a way to tell a for loop to skip THE next iteration(s) hence this workaround might be the way to go.
You define a counter variable that is filled with the number of iterations to skip if a specific case happens, e.g. sample value < 0. At the beginning of each iteration this counter vriable is checked whether the iteration should be skipped. If so, 1
is substracted from the counter and the iteration is skipped by using the keyword next
.
set.seed(123)
# assign vetor of desired length
len <- 10
ranPick <- numeric(len)
# use counter
skip_count <- 0
for (i in seq_len(len)){
# skip if already filled and counter is != 0
if (skip_count != 0) {
# reduce counter by 1 to indicate an iteration was skipped
skip_count <- skip_count - 1
print("skip")
next
}
ranPick[i] <- (round(rnorm(1, 1, 5)))
if (ranPick[i] < 0){
print("<0")
# min ensures the vector length does not exceed the desired length
ranPick[i:min(i+2, len)] <- ranPick[i]
# set counter to desired iterations to skip
skip_count <- 2
}
}
#> [1] "<0"
#> [1] "skip"
#> [1] "skip"
#> [1] "<0"
ranPick
#> [1] -2 -2 -2 0 9 1 2 10 3 -5
Note that I added a min()
call to ensure the outcome vector is not longer than the desired number of iterations. In this example, iteration 10 produces a negative number and hence -5
would be added 3 times, resulting in a vector of length 12.