How to Skip Current Iteration of for-Loop in R Programming (Example Code)

This article shows how to skip an iteration in case an if-condition is fulfilled in the R programming language.

Example: Don’t Run Specific Iterations within for-Loop Using next Function

for(iter in 1:3) {        # for-loop without next function
  cat(paste("My Iteration No.", iter, "n"))
}
# My Iteration No. 1 
# My Iteration No. 2 
# My Iteration No. 3
for(iter in 1:3) {        # for-loop with next function
  
  if(iter == 2) next
 
  cat(paste("My Iteration No.", iter, "n"))
}
# My Iteration No. 1 
# My Iteration No. 3

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top