Nested For-loop in R

R programming language allows using one loop inside another loop. In loop nesting, we can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa. The following section shows an example to illustrate the concept: 

Example:

R




# R Program to demonstrate the use of
# nested for loop
for (i in 1:3)
{
    for (j in 1:i)
    {
        print(i * j)
    }
}


Output: 

[1] 1
[1] 2
[1] 4
[1] 3
[1] 6
[1] 9

For loop in R

For loop in R Programming Language is useful to iterate over the elements of a list, data frame, vector, matrix, or any other object. It means the for loop can be used to execute a group of statements repeatedly depending upon the number of elements in the object. It is an entry-controlled loop, in this loop, the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.

Similar Reads

For loop in R Syntax:

for (var in vector) { statement(s) }...

Flowchart of For loop in R:

For loop in R...

Nested For-loop in R

...

Jump Statements in R

...

Creating Multiple Plots within for-Loop in R

...