Conditionally incrementing a loop counter: Take 2
Mike Jones wrote:
Hi, I am trying a for loop from 1 to 10 by 1. However, if a condition does not get met, I want to "throw away" that iteration. So if my loop is for (i in 1:10) and i is say, 4 and the condition is not met then I don't want i to go up to 5. Is there a way to do that? I can't seem to manually adjust i because from what I understand, R creates 10 long vector and uses that to "loops thru" and I'm not sure how to get at the index of that vector. Any suggestions? Thanks in advance.
Hi Mike,
Is this what you want?
i<-1
while(i < 11) {
if(runif(1) < 0.5) i<-i+1
print(i)
}
This increments if the condition is met, doesn't if it is not met.
Jim