How to Write a repeat-Loop in R Programming (Example Code)

In this article, I’ll show how to write and run a repeat-loop in the R programming language.

Example: Running repeat-Loop in R Programming Language

my_data <- 1                    # Specify example data object
repeat{                         # Entering repeat-loop
  my_data <- c(my_data,         # Creating some output
               my_data[length(my_data)] + 1)
  if(length(my_data) >= 8) {    # Check whether repeat-loop should continue
    break                       # End of repeat-loop
  }
}
my_data                         # Returning output
# [1] 1 2 3 4 5 6 7 8

Leave a Reply

Your email address will not be published. Required fields are marked *

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