R For Loop

Spread the love

This article aims to provide a detailed understanding of the for loop in R.

Introduction to for Loop

A for loop is a control flow statement that allows code to be executed repeatedly. The basic structure of a for loop in R is as follows:

for (value in sequence) {
  # Code to be executed

Here, value is a variable that takes each value from the sequence in each iteration, and the code inside the curly braces {} is executed for each value.

Consider this simple example:

for (i in 1:5) {

In this example, i is the variable, and 1:5 is the sequence. So i takes each value from 1 to 5, one by one, and for each i, print(i) is executed.

Using for Loops with Vectors and Lists

In R, sequences in for loops are not limited to numeric ranges. We can also use vectors and lists as sequences.

# Using a character vector
for (name in c("John", "Jane", "Joe")) {
  print(paste("Hello", name))

# Using a list
for (val in list(1:2, "a", TRUE, sqrt)) {

In the first example, name takes each value from the character vector, and in the second example, val takes each value from the list.

Nested for Loops

for loops can be nested within each other, meaning you can have a for loop inside another for loop. This is particularly useful when dealing with multidimensional data structures like matrices or data frames.

The basic structure of a nested for loop in R is as follows:

for (value1 in sequence1) {
  for (value2 in sequence2) {
    # Code to be executed

Here is an example where a nested for loop is used to print a 2D grid:

for (row in 1:3) {
  for (col in 1:3) {
    print(paste("Row:", row, "Column:", col))

In this example, for each row, the inner loop iterates over each col, and the coordinates are printed.

Control Flow in for Loops

Just like while loops, for loops also provide break and next keywords for controlling the flow of the loop:

  1. break: This keyword is used to exit the loop prematurely. When the program encounters break, it immediately exits the loop, regardless of the iteration.
  2. next: This keyword is used to skip the rest of the current iteration and proceed to the next iteration of the loop.

Here’s an example demonstrating the use of break and next in a for loop:

for (i in 1:10) {
  if (i == 6) {
  } else if (i %% 2 == 0) {

In this example, the loop skips even numbers with next and breaks when i is equal to 6, so it only prints the numbers 1, 3, and 5.

Common Pitfalls and Things to Remember

While using for loops in R, there are some things to keep in mind to avoid common pitfalls:

  • Avoid modifying the loop variable inside the loop: The loop variable should not be modified inside the loop, as it can lead to confusing results. The loop variable gets overwritten in each iteration by the next value from the sequence.
  • Beware of slow loops: In R, operations on whole data structures (like vectors and data frames) can often be much faster than loops. If a loop is slow, consider whether it could be replaced with a vectorized operation.
  • Preallocate memory for large objects: If you’re building a large object inside a loop (like a big vector), it’s more efficient to preallocate the memory in advance, rather than letting it grow incrementally in each iteration.


The for loop is a fundamental tool in the R programming language, facilitating repeated execution of a block of code. It’s especially useful when you know how many times a task needs to be performed, such as iterating over a specific sequence of numbers, characters, or logical values.

By allowing nesting, for loops enable the handling of multi-dimensional data structures, making them an invaluable tool in data analysis and manipulation. Moreover, through the use of break and next, you can exert fine control over the execution flow within the loop.

Posted in RTagged

Leave a Reply