R For Loop

The R For Loop is used to repeat a block of statements until there are no items in the Vector. It is one of the most used ones in any programming language.

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

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

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

  • It starts with an Object, means it will iterate Vector, and then it will assign the first item to value. Example, 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.

R For Loop Flow Chart

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

FOR LOOP FLOW CHART

The execution process of the R for loop is:

  1. Initialization: We initialize the variable(s) here. Example x =1.
  2. Items in the Sequence/Vector: It will check the items, 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 that block.
  3. After completing every iteration, it will traverse to the next item to the given number of times.
  4. Again it will check the new items in vector. As long as the items are available, the statements inside it will be executed.

R For Loop Example

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

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

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

In the next line, we used a print statement outside of it. This statement will be executed once the compiler exit.

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 ---")
R For Loop 1

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

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

First Iteration

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

Second Iteration

  • In 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 it.

Third Iteration

  • Country = ‘Japan’.
  • It means there are some items in it, so it executes the statement.

Fourth Iteration

  • Country = ‘U S A’.
  • It means there are some items in the vector, so it executes print statement.

Fifth Iteration of an R For Loop

  • Country = ‘Australia’, condition tested.
  • It means there are some items in it, so it executes the print statement.

Sixth Iteration

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

Next iteration, there are no items in this to assign to the Country variable. So, it will exit.

For Loop Example 2

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

numbers <- c(1:10)

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