Loop Through List in R (Example) | while- & for-Loop Over Lists

 

In this R programming tutorial you’ll learn how to run a for-loop to loop through a list object.

The tutorial looks as follows:

Let’s start right away:

 

Introduction of Example Data

Let’s first create some example data in R:

my_list <- list(c(6, 1, 5, 4, 1),    # Create example list
                "XXXX",
                letters[1:3])
my_list                              # Print example list
# [[1]]
# [1] 6 1 5 4 1
# 
# [[2]]
# [1] "XXXX"
# 
# [[3]]
# [1] "a" "b" "c"

The previous output of the RStudio console shows the structure of our example data – It’s a list consisting of three different list elements.

 

Example: for-Looping Over List Elements in R

In this Example, I’ll explain how to loop through the list elements of our list using a for-loop in R. Within each iteration of the loop, we are printing the first entry of the corresponding list element to the RStudio console:

for(i in 1:length(my_list)) {        # Loop from 1 to length of list
  print(my_list[[i]][1])             # Printing some output
}
# [1] 6
# [1] "XXXX"
# [1] "a"

This example has shown how to loop over a list using a for-loop. However, it would also be possible to loop through a list with a while-loop or a repeat-loop.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I explain the examples of this tutorial in the video.

 

The YouTube video will be added soon.

 

Also, you might read some of the other articles on this website.

 

To summarize: In this article, I showed how to loop over list elements in R. Don’t hesitate to tell me about it in the comments below, if you have further questions or comments.

 

6 Comments. Leave new

  • for(i in 1:length(my_list)) { # Loop from 1 to length of list
    print(my_list[[i]][1]) # Printing some output
    }

    what does the i represent, and the one in the second line ” print(my_list[[i]][1]) “

    Reply
    • Hi Ted,

      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
  • Hi All.
    I have a list containing several data frames and I’d like to apply a function to all data frames in the list, however, just in columns 7 and 8.
    Is it possible to use the “Lapply” function in these specific columns in each data frame?
    I tried the following but had no success at all.
    lapply(mylist[, 7:8], my_func)
    Thank you.

    Reply
    • Hello Carlos,

      I was on vacation. Now I can return to the comments. Yes, it’s possible to use lapply to apply a function to specific columns across data frames in a list. However, your approach is incorrect because you’re trying to subset the list as if it was a data frame. YYou can use the lapply function in combination with anonymous functions to achieve this. Here’s a step-by-step approach:

      Loop through each data frame in the list using lapply.
      For each data frame, apply the function my_func to columns 7 and 8.

      See below.

      set.seed(10)
      # Create three sample data frames
      df1 <- data.frame(a = rnorm(10),
                        b = rnorm(10),
                        c = rnorm(10),
                        d = rnorm(10),
                        e = rnorm(10),
                        f = rnorm(10),
                        g = rnorm(10),
                        h = rnorm(10))
       
      df2 <- data.frame(a = rnorm(10),
                        b = rnorm(10),
                        c = rnorm(10),
                        d = rnorm(10),
                        e = rnorm(10),
                        f = rnorm(10),
                        g = rnorm(10),
                        h = rnorm(10))
       
      df3 <- data.frame(a = rnorm(10),
                        b = rnorm(10),
                        c = rnorm(10),
                        d = rnorm(10),
                        e = rnorm(10),
                        f = rnorm(10),
                        g = rnorm(10),
                        h = rnorm(10))
       
      # Combine them into a list
      mylist <- list(df1, df2, df3)
      mylist
      # [[1]]
      #              a           b          c           d           e          f          g          h
      # 1   0.01874617  1.10177950 -0.5963106 -1.85374045  1.08655140 -0.4006375 -1.2375945  0.3809222
      # 2  -0.18425254  0.75578151 -2.1852868 -0.07794607 -0.76254488 -0.3345566 -0.4561763 -1.4304273
      # 3  -1.37133055 -0.23823356 -0.6748659  0.96856634 -0.82866254  1.3679540 -0.8303227 -1.0484455
      # 4  -0.59916772  0.98744470 -2.1190612  0.18492596  0.83447390  2.1377671  0.3401156 -0.2185036
      # 5   0.29454513  0.74139013 -1.2651980 -1.37994358 -0.96765199  0.5058193  1.0663764 -1.4899362
      # 6   0.38979430  0.08934727 -0.3736616 -1.43551436 -0.02881534  0.7863424  1.2161258  1.1727063
      # 7  -1.20807618 -0.95494386 -0.6875554  0.36208723  0.23252515 -0.9022119  0.7356907 -1.4798270
      # 8  -0.36367602 -0.19515038 -0.8721588 -1.75908675 -0.30120868  0.5328970 -0.4812086 -0.4303878
      # 9  -1.62667268  0.92552126 -0.1017610 -0.32454401 -0.67761458 -0.6458943  0.5627448 -1.0516386
      # 10 -0.25647839  0.48297852 -0.2537805 -0.65156299  0.65522764  0.2909875 -1.2463197  1.5225863
      # 
      # [[2]]
      #              a           b          c           d           e          f           g          h
      # 1   0.59282805 -0.41635467 -0.7618043 -1.28015460 -0.48136561  1.4292128 -1.67533218 -0.3911042
      # 2  -0.22266151 -0.19148234  0.4193754  1.12886823  0.20288178  0.6334359 -1.20518539 -0.2498675
      # 3   0.71289428  0.06954478 -1.0399434 -0.46413453 -0.03173974 -1.9968156 -1.96325249  1.1551047
      # 4   0.71660083  1.15534832  0.7115740 -0.31576021 -1.19558030 -0.6818322  1.47075231 -0.8647272
      # 5   0.44024186  0.59495735 -0.6332130  0.92429315  0.62368124 -0.4600555  0.37247234 -0.8666783
      # 6   0.15883062 -1.41964511  0.5631747  0.07714472 -0.91480448 -0.9830692  1.06587933 -2.3210170
      # 7   0.65976414 -1.60667725  0.6609867  1.03992361  0.24875801  0.4953317  0.53064987  0.6088302
      # 8   2.22051966  0.89292590 -1.6580509  0.74188621 -1.06262279  0.7258175  0.10198345  1.1500060
      # 9  -1.18394507  0.14816796  1.0281680  1.25554486 -0.36398225  0.6672987  1.33778247 -1.1995977
      # 10 -0.07395583  1.22702839  1.1279536  0.95091897 -1.20699485  0.9547864  0.08723477 -1.5800008
      # 
      # [[3]]
      #             a          b           c          d          e          f           g           h
      # 1   0.6531662 -1.1373999  0.42551309  0.1130294  1.2155138  0.1418803 -0.32922472 -0.27882350
      # 2  -0.5494085 -0.4146453  0.64350004  0.9953319  0.3308765  1.2617151 -0.28282162 -1.26673154
      # 3   0.5210545  0.1439343 -1.36030614 -0.6811514  1.3902751 -0.4315003  0.43242913 -0.24914839
      # 4  -0.6994031  1.0620243 -0.19850611 -1.2770572  0.8720470 -1.8227126 -0.30760710  0.01798841
      # 5  -0.4389093 -0.5707939  0.61930268 -1.4686977 -1.0808170  0.3525440 -0.05663631  0.37707273
      # 6  -0.6773193  1.2771814  2.06820961 -0.3134741  0.4958216 -1.3484514  0.73351542  0.79600856
      # 7   0.9591412  0.2282893 -0.30528475 -1.7036595  1.0526276  0.7076883  0.09731162 -0.84067742
      # 8  -1.4681733 -0.3088131  0.28124561 -1.3505147 -1.2746500 -0.4108909  1.63089174 -2.20547175
      # 9   0.1837639  0.9598291  0.69131734 -1.1020937 -0.1936667 -0.4460452  0.56061070 -1.12805599
      # 10 -1.4351472  0.5488224  0.04636144 -1.0995430 -1.2950836 -1.0411563  1.32956476 -1.34130996
       
       
      # Define the function you want to apply
      my_func <- function(x) {
        # Your function logic here, for example:
        return(x*2)
      }
       
      # Use lapply to modify columns 7 and 8 in each data frame
      modified_list <- lapply(mylist, function(df) {
        df[, 7:8] <- lapply(df[, 7:8], my_func)
        return(df)
      })
       
      modified_list
       
      # [[1]]
      #              a           b          c           d           e          f          g          h
      # 1   0.01874617  1.10177950 -0.5963106 -1.85374045  1.08655140 -0.4006375 -2.4751889  0.7618444
      # 2  -0.18425254  0.75578151 -2.1852868 -0.07794607 -0.76254488 -0.3345566 -0.9123526 -2.8608545
      # 3  -1.37133055 -0.23823356 -0.6748659  0.96856634 -0.82866254  1.3679540 -1.6606453 -2.0968910
      # 4  -0.59916772  0.98744470 -2.1190612  0.18492596  0.83447390  2.1377671  0.6802313 -0.4370071
      # 5   0.29454513  0.74139013 -1.2651980 -1.37994358 -0.96765199  0.5058193  2.1327528 -2.9798725
      # 6   0.38979430  0.08934727 -0.3736616 -1.43551436 -0.02881534  0.7863424  2.4322517  2.3454126
      # 7  -1.20807618 -0.95494386 -0.6875554  0.36208723  0.23252515 -0.9022119  1.4713813 -2.9596540
      # 8  -0.36367602 -0.19515038 -0.8721588 -1.75908675 -0.30120868  0.5328970 -0.9624172 -0.8607756
      # 9  -1.62667268  0.92552126 -0.1017610 -0.32454401 -0.67761458 -0.6458943  1.1254895 -2.1032773
      # 10 -0.25647839  0.48297852 -0.2537805 -0.65156299  0.65522764  0.2909875 -2.4926394  3.0451727
      # 
      # [[2]]
      #              a           b          c           d           e          f          g          h
      # 1   0.59282805 -0.41635467 -0.7618043 -1.28015460 -0.48136561  1.4292128 -3.3506644 -0.7822084
      # 2  -0.22266151 -0.19148234  0.4193754  1.12886823  0.20288178  0.6334359 -2.4103708 -0.4997350
      # 3   0.71289428  0.06954478 -1.0399434 -0.46413453 -0.03173974 -1.9968156 -3.9265050  2.3102095
      # 4   0.71660083  1.15534832  0.7115740 -0.31576021 -1.19558030 -0.6818322  2.9415046 -1.7294545
      # 5   0.44024186  0.59495735 -0.6332130  0.92429315  0.62368124 -0.4600555  0.7449447 -1.7333567
      # 6   0.15883062 -1.41964511  0.5631747  0.07714472 -0.91480448 -0.9830692  2.1317587 -4.6420341
      # 7   0.65976414 -1.60667725  0.6609867  1.03992361  0.24875801  0.4953317  1.0612997  1.2176603
      # 8   2.22051966  0.89292590 -1.6580509  0.74188621 -1.06262279  0.7258175  0.2039669  2.3000121
      # 9  -1.18394507  0.14816796  1.0281680  1.25554486 -0.36398225  0.6672987  2.6755649 -2.3991953
      # 10 -0.07395583  1.22702839  1.1279536  0.95091897 -1.20699485  0.9547864  0.1744695 -3.1600015
      # 
      # [[3]]
      #             a          b           c          d          e          f          g           h
      # 1   0.6531662 -1.1373999  0.42551309  0.1130294  1.2155138  0.1418803 -0.6584494 -0.55764699
      # 2  -0.5494085 -0.4146453  0.64350004  0.9953319  0.3308765  1.2617151 -0.5656432 -2.53346309
      # 3   0.5210545  0.1439343 -1.36030614 -0.6811514  1.3902751 -0.4315003  0.8648583 -0.49829679
      # 4  -0.6994031  1.0620243 -0.19850611 -1.2770572  0.8720470 -1.8227126 -0.6152142  0.03597683
      # 5  -0.4389093 -0.5707939  0.61930268 -1.4686977 -1.0808170  0.3525440 -0.1132726  0.75414545
      # 6  -0.6773193  1.2771814  2.06820961 -0.3134741  0.4958216 -1.3484514  1.4670308  1.59201712
      # 7   0.9591412  0.2282893 -0.30528475 -1.7036595  1.0526276  0.7076883  0.1946232 -1.68135483
      # 8  -1.4681733 -0.3088131  0.28124561 -1.3505147 -1.2746500 -0.4108909  3.2617835 -4.41094350
      # 9   0.1837639  0.9598291  0.69131734 -1.1020937 -0.1936667 -0.4460452  1.1212214 -2.25611198
      # 10 -1.4351472  0.5488224  0.04636144 -1.0995430 -1.2950836 -1.0411563  2.6591295 -2.68261992

      Best,
      Cansu

      Reply
  • Hello Cansu.

    Thank you so much. Very clear explanation and helps me a lot.

    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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top