For Loops Can Be Nested in R

Developed with
For Loops Can Be Nested in R

This module looks at conditional statements in R, such as for loops, and how to repeat functions.   

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • When to use a for loop in R 
  • How to nest a for loop  
  • Built-in functions being vectorized
  • How to apply functions
  • How to use the family of apply functions  

Intended Audience 

Aimed at anyone who wishes to learn the R programming language.


No prior knowledge of R is assumed. You should already be familiar with basic programming concepts such as variables, scope, and functions. Experience of another scripting language such as Python or Perl would be an advantage. An understanding of mathematical concepts would be beneficial.


We welcome all feedback and suggestions - please contact us at to let us know what you think. 


For loops can be nested such that we have for example a for loop within another for loop. In this case here, I'm running a for loop across the alphabet characters b, c, d, e, stored inside this vector here. This is my iterator called alphabet. And I'm asking to loop over this for loop, and then outside of that I'm asking to loop over the larger for loop called number_KH is the iterator in this case, ranging from two to five. And as we run this output what we see is for each letter in the inner loop, we will be connecting it with a number from the outer loop. And as you can see the order is two b so we start on the larger loop on the outside, and we run through all of the letters on the inner loop, followed by the number three, and then all of the letters in the inner loop. The reason to show this on the screen is via the print statement, so it's useful to get an understanding of what's going on inside a nested for loop. The code block doesn't always have to print. We could've used the code block to create an output, say I had defined an output vector. Here I have initialized it in my global environment. I run a similar for loop, but this time I do not ask for any print statements. I merely past together the numbers and the letters in a temp output for each iteration. And then I concatenate or combine these into my output vector. So as we loop through each one of these combinations we won't print this out to the screen, but we will store it in the output vector in the global environment. So once I press Enter now, you'll see that this output vector now contains 17 entries, all representing the combination of the output vector and the temp output which are the combination of the letters and the numbers. We can print the output to the screen using the Print command to help us gauge an idea of what we had created before versus what we have created now. Where possible, the results object should be instantiated or initialized with the appropriate dimensions. Here I have not, but this is merely a demonstration. So where possible, the results object should be initialized with the appropriate dimensions. And as a closing point I'd like to state that in general loops tend to be slow, whilst in R we try to avoid loops and utilize the power of vectorization.


About the Author
Learning Paths

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.