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 a 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:

 

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


26 Comments. Leave new

  • Unnikrishnan C
    January 19, 2021 2:55 pm

    That was wonderful.
    My data.frame is like this.
    DATES <- rep(c("2021-01-18","2021-01-19"),each=5)
    STOCKDETAILS <- rep(c("MAJESCO","NSE","BUY",120000.00,"DEL","MASTEK","BSE","SELL",150000.00,"DEL"),times=1)
    I need to get dates only once, and the columns one by one. How to do that?
    Thanking you

    Reply
    • Unnikrishnan C
      January 21, 2021 4:52 am

      I have solved the problem myself filtering the dataframe and using an inner for loop and outer for loop. That resolves my problem. though it may not sound the best solution in R

      Reply
      • Hey Unnikrishnan,

        Thanks for another comment! Glad to hear that you found a solution for your problem. Indeed, multiple nested for-loops are often not the best/fastest solution, but as long as you don’t have problems due to computation time that shouldn’t matter too much.

        Regards,

        Joachim

        Reply
  • badara ceesay
    March 1, 2022 1:55 pm

    how to loop over 5 variable to find there summary statistic

    Reply
  • Pedro Maranhão
    March 9, 2022 12:13 pm

    Hello, Joachim
    I have a doubt, wish that you could help me
    I want to do filter (with the tidyverse’s function) to each unique value (character) in a column (df$id_regiao)
    with the result I want to make a data frame to each unique character
    How can I do that with a loop ?

    Reply
    • Hey Pedro,

      Could you illustrate what the desired output should look like? I’m afraid I don’t understand what you exactly want to do.

      Regards,
      Joachim

      Reply
  • sitaram sahoo
    May 26, 2022 10:16 am

    Hi,

    i’ve tried the while loop piece of code on the below data frame.

    df
    var1 var2 var3 var4
    1 21 27 23 21
    2 23 27 23 21
    3 23 28 26 22
    4 24 23 26 28
    5 15 12 18 19

    code : i repeat {
    + df[,i ] <- df[,i] + 100
    + i 2)
    + { break}
    + }

    and I the below.
    df
    var1 var2 var3 var4
    1 121 127 23 121
    2 123 127 23 121
    3 123 128 26 122
    4 124 123 26 128
    5 115 112 18 119

    seems like it didnot work on 2nd column only. would you care to provide a reason.

    thanks in advance

    Reply
  • Dear Joachim
    I need help in making a list from many data.fram (data.fram1 to data.fram25) and need to make a list of them using for loop.
    I would be happy if you help me.

    Reply
    • Hey Narges,

      I’m sorry for the late response, I just came back from vacation. Do you still need help with your question?

      Regards,
      Joachim

      Reply
  • Hi Joachim,

    Thank you for the wonderful explanations on looping. I have a sample dataset shown below fruits taken by students in a week. I have similar data with 1000 observations and 10 different columns. I want to club my data as a total of:
    1. Apple
    2. Banana
    3. Orange
    4. Apple + banana
    5. Apple + Orange
    6. banana+ Orange
    7. Apple + banana+ Orange

    Could you kindly show me how to do it? Thank you

    student_id Apple Banana orange
    1 0 1 0
    2 1 1 1
    3 1 0 1
    4 1 0 0
    5 0 0 0
    6 1 1 0
    7 0 1 1
    8 0 1 0

    Reply
  • Hi Joachim,

    How do i do loop for every 2×2 matrix and get the inverse value ot it?

    Reply
  • Hi Joachim,

    I’m wondering if you can help me a problem?

    I’m trying to iterate through a data frame using a for loop and identify negative values and multiply them by -10. I keep running into issues with the if condition I’m using to identify negative values. This is the code I’m using and the error that keeps occurring.

    df <- data.frame(x = letters[1:5], y = c(-4,-2,0,2,4), z = c(3,4,-5,6,-8))

    for(i in 2:ncol(df)){
    if(i <0){
    new_val <- df[,i]*-10
    df[, i] <- new_val-df[ ,i]
    }
    }

    Error msg: 'Error in if(df[,i] 1′

    Any help on how to fix this or an alternative method would be greatly appreciated.

    Thanks,

    Oriana

    Reply
    • Hey Oriana,

      I have just executed your code, and it worked without displaying any error messages.

      Are you certain that this is precisely the code you had problems with?

      Regards,
      Joachim

      Reply
    • I have almost the same code as Oriana (comment above).
      My code runs without error, but it doesn’t change my data frame column/s at all.
      I need to take all negative values make them positive and multiply by 100 i.e. multiply by -100.

      for(i in 2:ncol(df)) {
      if(i < 0) {
      df$y <- df$y*-100
      }
      }

      Any help would be greatly appreciated. Thank you

      Reply
      • Hello Jack,

        Your case is a bit different than given in the tutorial. Try the following code out.

        df <- data.frame(x = letters[1:5], y = c(-4,-2,0,2,4), z = c(3,4,-5,6,-8))
        df
         
        for(i in 2:ncol(df)){
          for(j in 1:nrow(df)){
         
            if(df[j,i] < 0) {
              df[ j, i] <- df[ j, i]*-100
            }
            else {
              df[ j, i] <- df[ j, i]
            }
          }
        }
         
        df

        Let me know if you have any further questions.

        Regards,
        Cansu

        Reply
  • Hi, Joachim.

    Is there a way to for-loop columns in a data frame with formulas.

    Here’s an example:

    Data

    Case Age Term Start Date
    1 34 2 2018
    2 21 3 2020
    3 43 5 2015

    Then, the additional columns should be
    Case — Y2015 Y2016 Y2017 Y2018 Y2019 Y2020
    1 0 0 0 34 35 36
    2 0 0 0 0 0 21
    3 43 44 45 46 47 0

    Thank you!

    – bulet

    Reply
  • great exercises. I got a lot of benefits from Youtube videos as well website. Thank you so much.
    Just wondering how do iterate through columns by data.
    e.g. if I have 1:N columns and want to iterate through columns using a specific date

    Reply
    • Hi Hamad,

      Thank you so much for the kind words, glad you find my tutorials helpful!

      I’m sorry for the delayed reply. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?

      Regards,
      Joachim

      Reply

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.

Top