How to Create a Nested For Loop in R?
Last Updated :
02 Feb, 2022
A loop in a programming language is a sequence of instructions executed one after the other unless a final condition is met. Using loops is quite frequent in a program.
Need of a loop
Let us consider a scenario where we want to print natural numbers from 1 to 3. We can simply print them one by one. But suppose the range is high, 1 to 100 or 1 to 10000 or even 1 to 100000, then printing them one by one is a tedious task. In such cases, loops are quite efficient and also improve the readability of the source code. In R, loops are broadly classified into three categories: for, while, and repeat. This article focuses upon the working of For-loop in R.
For loop in R:
For loop is one of the commonly used types of loops. It is a control statement that is used in scenarios where multiple statements are required to be executed. In R, for loop follows the below syntax.
Syntax:
for (element in sequence) {
// body
}
Here, an element is the actual value in the sequence one after the another and starting from the beginning.
Nested for loop in R:
A nested for-loop has a for-loop inside of another for-loop. For each of the iteration in the outer for-loop, the inner loop will be executed unless a final condition is met for the inner loop. Once an inner for-loop is executed for a particular outer iteration then the outer for-loop goes for the next iteration and now the inner loop will be executed for this iteration. This process repeats itself till the final condition is met for the outer for-loop. Nested for-loop can be visualized as iterating over integer coordinates one by one in a two-dimensional space. For example, printing
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
(3,0) (3,1) (3,2)
Below is the syntax of nested for-loop in R.
Syntax:
for (element1 in sequence1) {
for(element2 in sequence2)
// body
}
Example 1: In the below program we are iterating over 0 to 3 in the outer loop and for each value in the outer for-loop, we are iterating over 0 to 3 in the inner for-loop.
R
# R program to illustrate the working of
# nested for-loop
# Iterating over 0 to 3 in the outer
# for-loop
for (x in 0:3){
# Iterating over 0 to 3 in the inner
# for-loop
for (y in 0:3)
{
# Print x and y
print(paste("(", x, ",", y, ")"))
}
}
Output:
Example 2: In the below program we are iterating over 1 to 5 in the outer loop and for each of the values in the outer for-loop, we are iterating over 1 to 5 in the inner for-loop and simultaneously printing the sum of the two values.
R
# R program to illustrate the working
# of nested for-loop
# Iterating over 1 to 5 in the outer
# for-loop
for(number1 in 1:5)
{
# Iterating over 1 to 5 in the inner
# for-loop
for(number2 in 1:5)
{
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}
Output:

Next statement:
Most of the languages provide functionality using which we can skip the current iteration at the moment and go for the next iteration if it exists. For this purpose, we have the "next" statement in R.
Example 1: In the below program we are skipping the inner for-loop if the value of number1 is equal to one.
R
# R program to illustrate the working
# of next statement in nested for-loop
# Iterating over outer loop
for(number1 in 1:5)
{
if(number1 == 1)
next
# Iterating over outer loop
for(number2 in 1:5)
{
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}
Output:
Example 2: In the below program we are skipping the print statement if the value of number2 is equal to one in the inner for-loop.
R
# R program to illustrate the working
# of next statement in nested for-loop
# Iterating over outer loop
for(number1 in 1:5)
{
# Iterating over outer loop
for(number2 in 1:5)
{
# If number1 is equal to 1
# Then skip the print statement below
# and move to the next iteration
if(number2 == 1)
next
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}
Output:

Break statement:
Most of the languages provide functionality using which we can stop the iteration of for-loop at the moment and come out of the current for-loop scope. For this purpose, we have the "break" statement in R. Break statement in R is the same as the break statement in C or C++.
Example 1: In the below program the flow of control of the outer for-loop comes out of the scope once the value of number1 reaches towards 2.
R
# R program to illustrate the working
# of break statement in nested for-loop
# Iterating over 1 to 5 in outer for-loop
for(number1 in 1:5)
{
# If number1 is equal to 2
# then break the flow of control
# and come out of the outer loop
if(number1 == 2)
break
# Iterating over 1 to 5 in inner for-loop
for(number2 in 1:5)
{
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}
Output:
Example 2: In the below program the flow of control of the inner for-loop comes out of the scope once the value of number2 reaches towards 2.
R
# R program to illustrate the working of
# break statement in nested for-loop
# Iterating over 1 to 5 in the outer for-loop
for(number1 in 1:5)
{
# Iterating over 1 to 5 in the inner for-loop
for(number2 in 1:5)
{
# If number2 is equal to 2 then stop
# the flow of control
# and come out of the inner for-loop at the moment
if(number2 == 2)
break
# Print the sum of number1 and number2
print(paste(number1, "+", number2, "=",
number1 + number2));
}
}
Output:
Similar Reads
How to create a list in R
In this article, we will discuss What is a list and various methods to create a list using R Programming Language. What is a list?A list is the one-dimensional heterogeneous data i.e., which stores the data of various types such as integers, float, strings, logical values, and characters. These list
2 min read
How To Use A For Loop In R
For loops in R is a fundamental programming construct that allows you to repeat a block of code a specified number of times or for a given range of elements. They are essential for automating repetitive tasks, manipulating data, and performing various computational operations. The basic syntax of a
3 min read
How to create a matrix in R
In this article, we will discuss What is a matrix and various methods to create a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data set that collects rows and columns. The matrix stores the data in rows and columns format. It is possible to access the data i
3 min read
How to Create a Population Pyramid in R?
In this article, we will discuss how to create a population pyramid in the R Programming Language. A population pyramid is also known as an age-sex pyramid. It helps us to visualize the distribution of a population by age group and sex. It generally takes the shape of a pyramid. In the population py
4 min read
How to Create a Three Way Table in R
A three-way table, also known as a three-dimensional contingency table, is a tabular representation of the joint frequencies of three categorical variables. It provides a way to analyze the relationships between three categorical variables simultaneously. In this article, we will study different app
6 min read
How to create dataframe in R
Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
How to create, index and modify Data Frame in R?
In this article, we will discuss how to create a Data frame, index, and modify the data frame in the R programming language. Creating a Data Frame:A Data Frame is a two-dimensional labeled data structure. It may consist of fields/columns of different types. It simply looks like a table in SQL or lik
4 min read
How to Create Tables in R?
In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read
How to Create a Two Way Table in R?
In this article, we will create a two-way table in R programming language. A two-way table is used to display frequency for two categorical variables. The rows represent the categorical features and the column represents frequency. We can create two-way table using as.table() method. as.table() func
2 min read
Nested for Loop to Print a Pattern.
A nested for loop in R is used when we want to iterate through multiple dimensions, such as rows and columns of a matrix, or for creating patterns. we will discuss how to print multiplication tables up to a certain number with its working example in the R Programming Language using R for loop condit
3 min read