# Nested Loop in R (2 Examples)

In this R tutorial you’ll learn how to **nest multiple loops**.

The article will consist of two examples for the nesting of while- and for-loops. To be more specific, the content is structured as follows:

So without further additions, let’s dive right in:

## Example 1: Creating Nested for-Loop in R

In Example 1, I’ll show how to create two nested for-loops in R.

In this example, we are running three iterations of the outer for-loop with the index i and five iterations of the inner for-loop with the index j.

In each iteration we are printing a sentence to the RStudio console that contains the current index numbers of both indices.

Let’s do this:

for(i in 1:3) { # Head of outer loop for(j in 1:5) { # Head of inner loop print(paste("This is iteration i =", i, "and j =", j)) # Some output } } # [1] "This is iteration i = 1 and j = 1" # [1] "This is iteration i = 1 and j = 2" # [1] "This is iteration i = 1 and j = 3" # [1] "This is iteration i = 1 and j = 4" # [1] "This is iteration i = 1 and j = 5" # [1] "This is iteration i = 2 and j = 1" # [1] "This is iteration i = 2 and j = 2" # [1] "This is iteration i = 2 and j = 3" # [1] "This is iteration i = 2 and j = 4" # [1] "This is iteration i = 2 and j = 5" # [1] "This is iteration i = 3 and j = 1" # [1] "This is iteration i = 3 and j = 2" # [1] "This is iteration i = 3 and j = 3" # [1] "This is iteration i = 3 and j = 4" # [1] "This is iteration i = 3 and j = 5" |

for(i in 1:3) { # Head of outer loop for(j in 1:5) { # Head of inner loop print(paste("This is iteration i =", i, "and j =", j)) # Some output } } # [1] "This is iteration i = 1 and j = 1" # [1] "This is iteration i = 1 and j = 2" # [1] "This is iteration i = 1 and j = 3" # [1] "This is iteration i = 1 and j = 4" # [1] "This is iteration i = 1 and j = 5" # [1] "This is iteration i = 2 and j = 1" # [1] "This is iteration i = 2 and j = 2" # [1] "This is iteration i = 2 and j = 3" # [1] "This is iteration i = 2 and j = 4" # [1] "This is iteration i = 2 and j = 5" # [1] "This is iteration i = 3 and j = 1" # [1] "This is iteration i = 3 and j = 2" # [1] "This is iteration i = 3 and j = 3" # [1] "This is iteration i = 3 and j = 4" # [1] "This is iteration i = 3 and j = 5"

The previous RStudio console output illustrates the sequence of how our nested for-loops were executed.

## Example 2: Nesting for-Loop in while-Loop

It is also possible to nest different types of loops. Example 2 explains how to nest a for-loop into a while-loop.

First, we have to create a data object containing our running index:

i <- 1 # Create running index |

i <- 1 # Create running index

Then, we can run our nested while- and for-loops as shown below:

while(i <= 3) { # Head of while-loop for(j in 1:5) { # Head of inner loop print(paste("This is iteration i =", i, "and j =", j)) # Some output } i <- i + 1 # Increase index } # [1] "This is iteration i = 1 and j = 1" # [1] "This is iteration i = 1 and j = 2" # [1] "This is iteration i = 1 and j = 3" # [1] "This is iteration i = 1 and j = 4" # [1] "This is iteration i = 1 and j = 5" # [1] "This is iteration i = 2 and j = 1" # [1] "This is iteration i = 2 and j = 2" # [1] "This is iteration i = 2 and j = 3" # [1] "This is iteration i = 2 and j = 4" # [1] "This is iteration i = 2 and j = 5" # [1] "This is iteration i = 3 and j = 1" # [1] "This is iteration i = 3 and j = 2" # [1] "This is iteration i = 3 and j = 3" # [1] "This is iteration i = 3 and j = 4" # [1] "This is iteration i = 3 and j = 5" |

while(i <= 3) { # Head of while-loop for(j in 1:5) { # Head of inner loop print(paste("This is iteration i =", i, "and j =", j)) # Some output } i <- i + 1 # Increase index } # [1] "This is iteration i = 1 and j = 1" # [1] "This is iteration i = 1 and j = 2" # [1] "This is iteration i = 1 and j = 3" # [1] "This is iteration i = 1 and j = 4" # [1] "This is iteration i = 1 and j = 5" # [1] "This is iteration i = 2 and j = 1" # [1] "This is iteration i = 2 and j = 2" # [1] "This is iteration i = 2 and j = 3" # [1] "This is iteration i = 2 and j = 4" # [1] "This is iteration i = 2 and j = 5" # [1] "This is iteration i = 3 and j = 1" # [1] "This is iteration i = 3 and j = 2" # [1] "This is iteration i = 3 and j = 3" # [1] "This is iteration i = 3 and j = 4" # [1] "This is iteration i = 3 and j = 5"

Note that the output of our nested while- and for-loops is exactly the same as in Example 1 were we used only for-loops. We could also use repeat-loops to generate the same result.

## Video, Further Resources & Summary

Would you like to know more about nesting loops? Then you might want to watch the following video of my YouTube channel. I show the R programming syntax of the present article in the video:

*The YouTube video will be added soon.*

Furthermore, you could have a look at the related tutorials of my website:

In summary: In this tutorial, I illustrated how to **nest loops** in R programming. If you have any additional questions, please let me know in the comments.

**5**/

**5**(

**4**votes )