When should you apply “next” statement in R? When is it appropriate to use the “next” statement in R?

Category: R LanguageWhen should you apply “next” statement in R? When is it appropriate to use the “next” statement in R?
Editor">Editor Staff asked 2 years ago

When should you apply “next” statement in R? When is it appropriate to use the “next”
statement in R?

1 Answers
Editor">Editor Staff answered 2 years ago

A data scientist will use next to skip an iteration in a loop. As an example:

val1 <- 1:30
> for(val in val1){
+ if(val == 25){
+ next
+ }
+ print(val)
+ }

 

This piece of code will iterate through the numbers from 1 to 30. It will skip 25 as we have used the next statement to skip the iteration from which we move on to the next value. We will obtain the output from 1-24 and 26-30.