[R] multiple "next" in for loops?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Nov 12 03:18:48 CET 2008


On 11/11/2008 8:16 PM, remko duursma wrote:
> 
> Dear R list,
> 
> 
> I was wondering if there is an easy fix to this problem (there are workarounds, as always):
> 
> Within a for loop, we can use "next" to skip to the next index,
> but how can we skip the next n indices?
> 
> So, I would like something that looks like;
> 
> for(i in 1:10){
> 
>    if(somecondition)next(5)
> 
> }
> 
> Is there a way?

Don't use a for loop, use a while loop:

i <- 1
while (i < 11) {
   if (somecondition) i <- i+5
   else {
     blah blah blah
     i <- i+1
   }
}

Duncan Murdoch



More information about the R-help mailing list