19

There doesn't seem to be any examples of 'next' usage in the control flow help page. I'd like it to skip to the next iteration based on a condition within the script.

Using the example below, let's say I don't want it to print, unless x[i] > 5, the expected output would be 5 through 10 on screen:

x <- 1:100
for(i in 1:10) {
# next(x[i] < 5) # Just for conceptualizing my question.
print(x[i])
}

How would I go about implementing the use of next to accomplish something like what's shown above?

Iterator
  • 20,250
  • 12
  • 75
  • 111
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • It would be nice if SO had a [tag:missing-documentation] tag. For all those little things where the R manual is incomplete. :) – Iterator Feb 14 '12 at 00:37
  • 3
    @Iterator: the documentation isn't _missing_ (`?"next"`), so maybe a "documentation-missing-example" tag? – Joshua Ulrich Feb 14 '12 at 01:26
  • @JoshuaUlrich Errm, I added a link to the documentation for this case. However, [as you know](http://stackoverflow.com/a/7327565/805808) there are other cases where the documentation is ... left as an exercise. :) However, there is a "code-example" (and "code-examples") tag. – Iterator Feb 14 '12 at 01:47
  • 1
    @JoshuaUlrich the problem was that there's not really a clear way to see an example of usage of `next` from the documentation. `example(next)` is similarly useless. – Brandon Bertelsen Feb 14 '12 at 01:49
  • Hmm, this makes me think of cases where I have a dataset, but elements are `NA`. Is my dataset half full or half empty? Same with documentation. Anyway, maybe the "code-examples" tag is better here. I still think there's a lot of undocumented functionality in R. :) – Iterator Feb 14 '12 at 01:59
  • 1
    @Iterator: That's what I meant to say. It _is_ documented but an example would really help because `next` and `break` aren't functions with arguments. I would also like if `?continue` took you to `?"next"`. – Joshua Ulrich Feb 14 '12 at 04:34
  • @JoshuaUlrich Maybe in the next iteration... :) – Iterator Feb 14 '12 at 06:41
  • I know it's V-day, but that was cheesy. – Brandon Bertelsen Feb 14 '12 at 06:59
  • 2
    @BrandonBertelsen Cheesy? You should meet my friend "multithreader" and his jokes. ;-) "OS: You've got nice threads." "Process: Wanna spawn?" "OS: Go fork yourself." – Iterator Feb 14 '12 at 17:35
  • @Iterator 7 years later, I just giggled at this horrible joke... – Brandon Bertelsen Mar 07 '19 at 12:41

2 Answers2

16

I will give you a complete example and a 'yes' but I am unsure what your questions is:

R> for (i in 1:10) {
+     if (i < 5) next
+     print(i)
+ }
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
10

To make this work, you need to test whether x < 5 and, if it is, go to next. next will, in turn (to quote the help page), "[halt] the processing of the current iteration and [advance] the looping index", starting back through the loop again.

x <- 1:100
for(i in 1:10) {
    if(x[i] < 5) next
    print(x[i])
}
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455