Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

How do I skip a loop iteration based on the value of the last iteration performed?

$
0
0

I am looping over a vector and I would like to know how to "skip" an iteration based on the last iteration.

For example: The first iteration will hit the 1, then the second will hit another 1, but I would like to skip that iteration IF the number is the same as the previous number.

Simple example:

vector <- c(1, 1, 1, 2, 3, 3, 4)

for (i in vector) {
print(i)
}

I do not want the iteration to print out "1" three times. I want it to skip to the next iteration until it reaches the 2. This needs to be dynamic as the real vector is much more complicated.

Vice versa, maybe I want to perform the next iteration, only if the iteration before it is the same.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>