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.
- Store Results of Loop in List
- Append to List in Loop
- for-Loop in R
- Loops in R
- The R Programming Language
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.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







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]) “
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
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.
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.
Best,
Cansu
Hello Cansu.
Thank you so much. Very clear explanation and helps me a lot.
Hello Carlos,
It is nice to hear that the solution helped.
Have a good one!
Cansu