R For Loop

The R For Loop is used to repeat a block of statements until there are no items in the Vector. For loop is one of the most used loops in any programming language. Let us see the syntax of the For Loop in R:

The basic syntax of the For loop in R Programming language is

for (val in vector)  {
     Statement 1
     Statement 2
     ………
     Statement N
}

If you observe the above syntax of for loop in R, Vector may be string or integer, or anything you want to iterate.

  • R For loop starts with Object, means it will iterate Vector, and then it will assign the first item to value. For instance, our vector values are A: E means, it will assign A to val.
  • Next, it will execute the statements inside the R For loop.
  • After completing the statements, it will go to the vector and assign the next value to the val.
  • The process will be repeated until there are no items in the vector.

R For Loop Flow Chart

The screenshot below will show you the flow chart behind the For Loop in R programming.

R FOR LOOP FLOW CHART

The execution process of the for loop in R is:

  1. Initialization: We initialize the variable(s) here. For example x =1.
  2. Items in the Sequence/ Vector: It will check for the items in Vector, and if there are items in sequence (True) then it will execute the statements inside the for loop in R. If there is no item in sequence ( False) then it will exit from the loop
  3. After completing every iteration, it will traverse to the next item in the vector.
  4. Again it will check for the new items in vector. As long as the items in vector, the statements inside the for loop will be executed.

R For Loop Example

In this example, we are going to explain how to use for loop to extract individual items or data from a Vector.

# For Loop Example

countries <- c('India', 'U K', 'Japan', 'U S A', 'Australia', 'China')

for (str in countries) {
  print(paste("Countries are:  ", str))
}
print("----This is Coming from Outside the For Loop---")

In this R Programming example, First, we declared Countries Vector and assigned the following values.

countries <- c('India', 'U K', 'Japan', 'U S A', 'Australia', 'China')

Here, we used the R for loop to iterate through the Countries vector and display the individual vector items.

for (str in countries) {
  print(paste("Countries are:  ", str))
}

In the next line, we used a print statement outside the for loop. This statement will be executed once the compiler exit from the for loop.

print("----This is Coming from Outside the For Loop---")
R For Loop 1

From the above Screenshot, you can observe that we used the Countries List for second for Loop in R example code.

countries <- c('India', 'U K', 'Japan', 'U S A', 'Australia', 'China')

First Iteration

  • For the first Iteration, Country = ‘India’.
  • It means there are some items in the vector, so it will execute the print statement inside the for loop.

Second Iteration

  • For the second Iteration of the R for loop, Country = ‘U K’.
  • It means there are some items in the vector, so it will execute the print inside the for loop.

Third Iteration

  • For the third Iteration, Country = ‘Japan’.
  • It means there are some items in the vector, so it executes the statement inside a for loop.

Fourth Iteration

  • For the fourth Iteration of for loop, Country = ‘U S A’.
  • It means there are some items in the vector, so it executes print statements inside a for loop.

Fifth Iteration

  • For the fifth Iteration, Country = ‘Australia’.
  • It means there are some items in the vector, so it executes the print statement inside the for loop.

Sixth Iteration

  • For the sixth Iteration, Country = ‘China’.
  • It means there are some items in the vector, so it will compile the print statement inside a for loop.

For the Seventh iteration, there are no items in the vector to assign for Country variable. So, it will exit from for loop.

For Loop Example 2

In this for loop in R example, we are going to explain the working functionality of the For Loop on integer vector.

# For Loop Example

numbers <- c(1:10)

for (num in numbers) {
  print(9 * num)
}
print("---- This is Coming from Outside the For Loop ---")
R For Loop 2