while-Loop in R (2 Examples) | Writing, Running & Using while-Statement
In this article you’ll learn how to write and use while-loops in the R programming language.
The page looks as follows:
Theoretical Workflow of while-Loops
Before we dive into the R code, let’s have a look at the theoretical workflow of while-loops. The following graphic illustrates the logic of while-loops:
while-loops start with a logical condition (i.e. the head of the while-loop) that decides whether the while-loop should keep running.
In case the logical condition is TRUE, a code block (i.e. the body of the while-loop) is executed. This process is repeated until the logical condition is FALSE.
Example 1: Writing while-Loop in R (Basics)
In this Example, I’ll illustrate how to write and run a while-loop in R programming. First, we have to create a data object that we can use in the while-loop later on:
my_x <- 5 # Creating data object my_x # Print data object # 5
Our data object simply contains the value 5.
Let’s assume that we want to run a while-loop as long as our data object is smaller than 10. Furthermore, we want to add +1 within each run of the while-loop and we want to print the intermediate value stored in our data object to the RStudio console.
For this, we can use the following R syntax:
while(my_x < 10) { # Starting while-loop (head) my_x <- my_x + 1 # Code block of while-loop (body) print(my_x) # Printing output } # [1] 6 # [1] 7 # [1] 8 # [1] 9 # [1] 10
As you can see based on the previous output of the RStudio console, our while-loop returned the values 6, 7, 8, 9, and 10. Afterwards, the while-loop stopped.
Example 2: Running while-Loop Through Data Frame Columns
In this Example, I’ll show how to use the while-loop statement to run over the variables of a data matrix. For this example, we are using the iris flower data frame. We can load and inspect iris as shown below:
data(iris) # Loading exemplifying data set head(iris) # Showing head of data in RStudio console # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
As you can see, the iris flower data frame contains five columns. The first four columns are numeric and the last column has the factor class.
Let’s assume that we want to iterate through these variables until we reach the first non-numeric variable. Until then, we want to add +50 to the values of the variable used in the current iteration.
In this case, we first have to specify a running index that is increase with each iteration. We are starting with the first column:
running_index <- 1 # Create index
Then, we can start running our while-loop as shown below:
while(is.numeric(iris[ , running_index])) { # Start of while-loop iris[ , running_index] <- iris[ , running_index] + 50 # Code block running_index <- running_index + 1 }
Let’s have a look at our updated data frame:
head(iris) # Showing head of updated data # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 55.1 53.5 51.4 50.2 setosa # 2 54.9 53.0 51.4 50.2 setosa # 3 54.7 53.2 51.3 50.2 setosa # 4 54.6 53.1 51.5 50.2 setosa # 5 55.0 53.6 51.4 50.2 setosa # 6 55.4 53.9 51.7 50.4 setosa
Each numeric variable was increased by 50.
Obviously you may make the content blocks within the while-loops more complex (e.g. detailed break and exit conditions or using functions with multiple conditions) to generate more advanced outputs.
However, I hope that the simplicity of the previous R programming examples helped to understand the logic of while-loops!
Video, Further Resources & Summary
Do you need further explanations on the content of this article? Then you may watch the following video of my YouTube channel. I explain the R programming code of this page in the video:
The YouTube video will be added soon.
Besides that, you may want to have a look at some of the related articles of this homepage. I have released numerous articles already.
- for-Loop in R
- repeat-Loop in R
- Skip for-Loop to Next Iteration
- Name Variables in for-Loop Dynamically
- Stop for-Loop when Warnings Appear
- Loops in R
- The R Programming Language
In this R post you learned how to apply while-loops. In case you have further questions, please tell me about it in the comments.