R Loop Through Data Frame Columns & Rows (4 Examples) | for-, while- & repeat-Loops

 

In this article you’ll learn how to loop over the variables and rows of a data matrix in the R programming language.

The article will consist of the following contents:

Let’s dive right in…

 

Example Data

As first step, I’ll have to create some data that we can use in the examples later on:

data <- data.frame(x1 = 1:5,    # Create example data
                   x2 = 6:10,
                   x3 = 11:15)
data                            # Print example data
#   x1 x2 x3
# 1  1  6 11
# 2  2  7 12
# 3  3  8 13
# 4  4  9 14
# 5  5 10 15

Have a look at the previous output of the RStudio console. It reveals that our example data has five rows and three columns. All variables are numeric.

 

Example 1: for-Loop Through Columns of Data Frame

In this Example, I’ll illustrate how to use a for-loop to loop over the variables of a data frame. First, let’s store our data frame in a new data object:

data1 <- data                   # Replicate example data

Now, we can use the for-loop statement to loop through our data frame columns using the ncol function as shown below:

for(i in 1:ncol(data1)) {       # for-loop over columns
  data1[ , i] <- data1[ , i] + 10
}

Let’s check how our data frame has changed:

data1                           # Print updated data
#   x1 x2 x3
# 1 11 16 21
# 2 12 17 22
# 3 13 18 23
# 4 14 19 24
# 5 15 20 25

As you can see based on the previous output of the RStudio console, we added +10 to each variable of our data frame.

 

Example 2: for-Loop Over Rows of Data Frame

It is also possible to apply for-loops to loop through the rows of a data frame. Example 2 explains how to use the nrow function for this task. First, let’s replicate our data:

data2 <- data                   # Replicate example data

Now, we can apply the following R code to loop over our data frame rows:

for(i in 1:nrow(data2)) {       # for-loop over rows
  data2[i, ] <- data2[i, ] - 100
}

In this example, we have subtracted -100 from each cell of our data matrix:

data2                           # Print updated data
#    x1  x2  x3
# 1 -99 -94 -89
# 2 -98 -93 -88
# 3 -97 -92 -87
# 4 -96 -91 -86
# 5 -95 -90 -85

 

Example 3: while-Loop Through Columns of Data Frame

This Example explains how to loop over the columns of a data frame using a while-loop. Again, we are first replicating our data:

data3 <- data                   # Replicate example data

Then, we also have to specify a running index that we can increase with each iteration of our while-loop:

i <- 1                          # Create running index

Now, we can start running our while-loop:

while(i <=2) {                  # Start while-loop
  data3[ , i] <- data3[ , i] + 100
  i <- i + 1
}

The result of the previous R syntax looks as follows:

data3                           # Print updated data
#    x1  x2 x3
# 1 101 106 11
# 2 102 107 12
# 3 103 108 13
# 4 104 109 14
# 5 105 110 15

As you can see, we have added +100 to the first two columns of our data. The third column was kept as in the original input data, since the while-loop stopped at the second column.

 

Example 4: repeat-Loop Through Columns of Data Frame

Similar to while-loops, we can also use a repeat-loop to loop over the variables of a data frame. Again, we have to replicate our data…

data4 <- data                   # Replicate example data

…and we have to specify a running index:

i <- 1                          # Create running index

Now, we can write and run a repeat-loop as shown below:

repeat{                         # Start repeat-loop
 
  data4[ , i] <- data4[ , i] + 100
  i <- i + 1
 
  if(i > 2) {
 
    break
  }
}

We have specified that our repeat-loop should break after the second iteration and, hence, only the first two variables where changed:

data4                           # Print updated data
#    x1  x2 x3
# 1 101 106 11
# 2 102 107 12
# 3 103 108 13
# 4 104 109 14
# 5 105 110 15

 

Video & Further Resources

Do you want to learn more about loops in R? Then you might have a look at the following video of my YouTube channel. I’m showing the examples of this article in the video:

 

The YouTube video will be added soon.

 

Furthermore, you may want to read some of the related articles of my website. I have published numerous articles already:

 

Summary: In this R tutorial you learned how to loop through multiple columns and rows of a data table. Don’t hesitate to tell me about it in the comments section below, in case you have any additional questions.

 



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.

Menu
Top