How to Write a Repeat Loop in R (3 Examples)

A repeat loop in R is a type of loop that allows a set of instructions to be repeated a fixed number of times or until a certain condition is met. This type of loop can be written using the repeat keyword followed by the instructions to be repeated, the number of times to repeat the instructions, and the keyword until followed by a condition that determines when the loop should stop. Examples of writing a repeat loop in R include using the repeat and while loops to iterate through a sequence of numbers, and the repeat and until loops to perform a set of instructions until a certain condition is met.


A repeat-loop in R can be used to repeatedly perform some action until a stop condition is reached.

You can use the following basic syntax to write a repeat-loop in R:

repeat{
  #do something

  if(some condition){
  break
  }
}

The following examples show how to use a repeat-loop in different scenarios.

Example 1: Print Values Until Specific Number is Reached

The following code shows how to use a repeat-loop to print values starting at 1 until 10 is reached:

#define starting value
x <- 0

#perform repeat-loop
repeat{
  x <- x+1
  print(x)

  if(x >= 10){
  break
  }
}

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Here’s how this code worked:

  • We defined the starting value as x = 0.
  • We told R to increment x by 1, then print x.
  • We told R to break the repeat-loop once x had reached a value of 10 or greater. 

Example 2: Add Values to Vector Until Specific Number is Reached

The following code shows how to use a repeat-loop to add values to a vector until a specific number is reached:

#define empty vector and starting value
data <- c()
x <- 0

#perform repeat-loop
repeat{
  x <- x+1
  data[x] <- x
  print(data)
  
  if(x >= 5){
  break
  }
}

[1] 1
[1] 1 2
[1] 1 2 3
[1] 1 2 3 4
[1] 1 2 3 4 5

Here’s how this code worked:

  • We created an empty vector and defined the starting value as x = 0.
  • We told R to increment x by 1, then insert the value of x into the xth position of the vector.
  • We told R to break the repeat-loop once x had reached a value of 5 or greater.

Example 3: Modify Values in Data Frame Until Specific Number is Reached

The following code shows how to use a repeat-loop to modify the values in an existing data frame until a specific number is reached:

#define data frame and starting value
df <- data.frame(A=c(6, 7, 2, 8),
                 B=c(2, 4, 5, 5))
x <- 0

#perform repeat-loop
repeat{
  x <- x+1
  df$A[x] <- x
  df$B[x] <- x * 2

  if(x >= nrow(df)){
  break
  }
}

#view resulting data frame
df

  A B
1 1 2
2 2 4
3 3 6
4 4 8

  • We created an empty data frame and defined the starting value as x = 0.
  • We told R to increment x by 1, then insert the value of x into the xth position of column A and insert the value of x*2 into the xth position of column B.
  • We told R to break the repeat-loop once x had reached a value equal to or greater than the number of rows in the data frame.

The following tutorials explain how to perform other common tasks in R:

x