How To Use While Loops in R To Iterate Across Data Frames

Loops are one of the most fundamental parts of any programming language. And saying that they’re an essential part of languages specifically targeting data science, like R, would be an understatement. In fact, looping and iterating over data is so important that R even has a number of functions that can automate and optimize the process. But there are always going to be times when you need to get to the core of your data and manually put loops together. So how would we use an R-specific data type like a data frame with a while loop?

An Overview of While Loops

A while loop implemented in R programming is fairly similar to what you’ll find in other programming languages. While loops are essentially just a way of stating that we need to keep doing something while a test expression is evaluated. For example, we might want to move from one item to the next in a data frame while there are still items we haven’t looked at yet.

However, that doesn’t mean experienced programmers don’t have a lot to learn when jumping into R programming. This might seem like a contradictory statement at first. But learning to use loops often has very little to do with the loops themselves. Loops are inherently tied to the programming language you’re using them within. After all, a loop works by evaluating some of the most essential elements within a programming language. For example, looping over data frames in R means that we need to understand the basics of R’s frame implementations. We need to learn about both data types and how a while loop in R operates. But with that in mind, there are few better ways to learn about a language than by jumping into the code.

Starting Out With a Simple Example

We can start out with a simple example of how to form a while loop. You should also keep in mind that we’ll be going through a somewhat long form with the code in order to better illustrate the process. For example, we’ll use nrow to get the length of something we could determine just as easily by glancing at it. This more verbose style means that the techniques will scale up to larger projects. But with that in mind, try running the following code.

ourDataFrame <- data.frame(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30))

ourRowLength <-nrow(ourDataFrame)
ourRowPosition <- 1

while(ourRowPosition <=ourRowLength) {
print(ourDataFrame[ourRowPosition , 1])
ourRowPosition <- ourRowPosition + 1
}

We begin by creating and populating a new frame called ourDataFrame. Next, we define ourRowLength by using nrow to determine ourDataFrame’s length. We also define ourRowPosition as 1 to signify that we’re starting at the very beginning of a set.

The next code block is our actual while loop. We begin by supplying while with a logical condition. We provide while with a statement that it needs to test whether ourRowPosition is less than or equal to ourRowLength. We can essentially use almost any conditions here to signal the end of the while loop. It’s essentially shorthand to tell R’s interpreter that we want to do something until a condition has been met.

Next, we print out the current value we’re at in our iteration. We follow that up by increasing the iteration by one step. In this example, ourRowPosition serves as our loop variable or iteration variable. Keep in mind that we don’t necessarily need to always iterate by 1 either. We could even test a logical condition and increase or decrease the variable depending on the results. Finally, we finish off the while loop with a closing curly bracket. Of course, this is only the start of what we can do with while loops.

Taking While Loops to the Next Level

In the previous example, we considered basing iterative values on a conditional statement. But what if we wanted to flat-out exit the loop earlier than expected if a specific condition is met? We can do so with the break statement. Take a look at the following code.

ourDataFrame <- data.frame(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30))

ourRowLength <-nrow(ourDataFrame)
ourRowPosition <- 1

while(ourRowPosition <=ourRowLength) {
if (ourRowPosition==4) {
print(“breaking”)
break
}
print(ourDataFrame[ourRowPosition , 1])
ourRowPosition <- ourRowPosition + 1
}

We begin in a similar way to the prior example and define ourDataFrame, ourRowLength, and ourRowPosition. However, this time around we have an if conditional within the while loop. This conditional tests if ourRowPosition is equal to 4. If that condition is met than the while loop will print that it’s breaking and then exit out using the break statement. We can take this idea a step further by leveraging the loop syntax. Replace the while loop block with the following.

while(ourRowPosition <=ourRowLength && ourRowPosition !=4) {
print(ourDataFrame[ourRowPosition , 1])
ourRowPosition <- ourRowPosition + 1
}

This loop shows how we can use multiple conditions to evaluate our progress through &&. The && lets us chain a conditional check on whether or not ourRowPosition is 4 or not. It essentially gives us the same result as the earlier for loop but in a far tidier form.

At this point, we’ve moved in one direction over the frame. But what if we wanted to iterate over the entirety of it? We can easily accomplish that through nested loops. We essentially put one inner loop inside of an outer loop. Try running the following code.

ourDataFrame <- data.frame(
first = c (1,2,3,4,5,6,7,8,9,10),
second = c(11,12,13,14,15,16,17,18,19,20),
third = c(21,22,23,24,25,26,27,28,29,30))

ourRowLength <-nrow(ourDataFrame)
ourColLength <-ncol(ourDataFrame)
ourRowPosition <- 1
ourColPosition <- 1

while(ourColPosition <=ourColLength) {
while(ourRowPosition <=ourRowLength) {
print(ourDataFrame[ourRowPosition , ourColPosition])
ourRowPosition <- ourRowPosition + 1
}
ourColPosition <- ourColPosition + 1
ourRowPosition <- 1
}

Our initial declarations are similar to the earlier examples. The main difference is that we’re now defining values for both columns and rows. We begin the while loop in a similar way as well. However, immediately following the start of our for loop we go on to create a second inner loop.

The first loop iterates over the data frame’s columns. The second loop iterates over rows. At the end of every iteration through the row we set the read position back to 1 by resetting ourRowPosition. This lets us start from the beginning of the row with every new iteration. The combination of a nested format and while loops let us iterate through every element of ourDataFrame. And we can verify this thanks to the print statements which show a steady movement from 1 to 30.

Need more options? Check Out The articles below…

Scroll to top