The Complete Guide to While Loops in R


A while loop in R can be used to perform some action while some condition is met.

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

while(some condition is true) {
  #do some action
}

For example, you could use the following syntax to create a while loop that prints integer values from 0 to 10:

x <- 0

while(x <= 10){
  cat("the value is ", x, "\n")
  x <- x + 1
} 

This while loop uses the following logic:

  • Set the value of a variable named x to be 0.
  • For each loop, increment the value of x by 1.
  • While the value of x is less than 10, print the value.

Note: You must be careful when writing while loops. It is possible that a while loop will never stop if a condition is always true.

The following examples how to write while loops in several different scenarios in R.

Example 1: Use a While Loop with One Variable

The following code shows how to use a while loop with a single variable in R:

x <- 0

while(x < 10){
  cat("the value is ", x, "\n")
  x <- x + 1
}

the value is  0 
the value is  1 
the value is  2 
the value is  3 
the value is  4 
the value is  5 
the value is  6 
the value is  7 
the value is  8 
the value is  9

Here is how this code worked:

  • We defined the starting value as x = 0.
  • For each loop, we incremented the value of x by 1.
  • While the value of x is less than 10, print the value.

Note: We used the cat function in R to concatenate several objects together.

Example 2: Use a While Loop with One Variable and a Break Statement

We can also use a break statement within a while loop to break the loop if some condition is met.

The following code shows how to use a break statement within a while loop with a single variable in R:

x <- 0

while(x < 10){
  cat("the value is ", x, "\n")
  x <- x + 1
  if(x == 5){
    break
  }
}

the value is  0 
the value is  1 
the value is  2 
the value is  3 
the value is  4 

Here is how this code worked:

  • We defined the starting value as x = 0.
  • For each loop, we incremented the value of x by 1.
  • While the value of x is less than 10, print the value.
  • If at any point the value of x is equal to 5, break the loop.

In this particular example, once the value of x was equal to 5, the loop was broken and no numbers greater than 4 were printed.

Example 3: Use a While Loop with Multiple Variables

It’s also possible to use a while loop with multiple variables.

The following code shows how to use a while loop to multiply the corresponding values in two vectors and print the results:

i <- 1
x <- c(1, 2, 3, 4)
y <- c(2, 4, 6, 8)

while(i < 5){
  cat("the product is", x[i]*y[i], "\n")
  i <- i + 1
}

the product is 2 
the product is 8 
the product is 18 
the product is 32 

Here is how this code worked:

  • We defined the starting value as i = 0.
  • For each loop, we incremented the value of i by 1.
  • While the value of i is less than 5, print the product of the ith value from vector x multiplied by the ith value from vector y.

The output displays the product of the corresponding values in the two vectors.

Note: In this particular example we used a while loop with two variables, but you can include as many variables as you would like within the loop depending on how complex your operations may be.

Additional Resources

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

How to Create a Nested For Loop in R
How to Write a Nested If Else Statement in R
How to Loop Through Column Names in R

Leave a Reply

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