Using For Loops in R: A Guide

When working with loops, we use jump statements to either terminate the loop during a specific iteration or to skip over a particular iteration. The break statement is the most commonly used jump statement for terminating the loop during a specific iteration.


For Loops

The

for

code employs a loop to traverse through a sequence.

Example
for (x in 1:10) {

print(x)

}

Try it Yourself »

The

for

term operates differently compared to its usage in other programming languages. Instead of functioning as a keyword, it resembles an iterator method commonly seen in object-oriented programming languages.

The

for

loop allows us to perform a series of actions for each element present in a list, array, vector, etc.


Later on in the chapter, you will gain knowledge about items such as lists and vectors.

Example

Print every item in a list:

fruits <- list(“apple”, “banana”, “cherry”)

for (x in fruits) {

print(x)

}

Try it Yourself »

Example

Print the number of dices:

dice <- c(1, 2, 3, 4, 5, 6)

for (x in dice) {

print(x)

}

Try it Yourself »

The use of an indexing variable beforehand is not necessary for the

for

loop, unlike the

while

loop.


Break

By utilizing the

break

statement, we can halt the loop prior to it traversing all of the elements.

Example

Stop the loop at “cherry”:

fruits <- list(“apple”, “banana”, “cherry”)

for (x in fruits) {

if (x == “cherry”) {

break

}

print(x)

}

Try it Yourself »

The loop is programmed to end when the variable specified in

x

matches the string “cherry” and the

break

statement is executed. As a result, the loop will stop processing at “cherry”.



Next

By utilizing the

next

statement, we can skip over an iteration within the loop without causing it to terminate.

Example

Skip “banana”:

fruits <- list(“apple”, “banana”, “cherry”)

for (x in fruits) {

if (x == “banana”) {

next

}

print(x)

}

Try it Yourself »

Upon encountering the term “banana” during the loop, the program will proceed to skip it and move on to the next iteration.


Yahtzee!

If .. Else Combined with a For Loop

To provide an illustration, suppose we engage in a game of Yahtzee!

Example

Output the word “Yahtzee!” only when the dice displays a value of 6.

dice <- 1:6

for(x in dice) {

if (x == 6) {

print(paste(“The dice number is”, x, “Yahtzee!”))

} else {

print(paste(“The dice number is”, x, “Not Yahtzee”))

}

}

Try it Yourself »

When the loop iterates through the values 1 to 5, it displays “No Yahtzee” followed by the corresponding number. However, once it reaches the value 6, it prints “Yahtzee!” and the associated number.


Nested Loops

It is also possible to nest a loop within another loop.

Example

Generate a list of all the fruits along with their corresponding adjectives.

adj <- list(“red”, “big”, “tasty”)

fruits <- list(“apple”, “banana”,
“cherry”)

for (x in adj) {

for (y in fruits) {

print(paste(x, y))

}

}

Try it Yourself »

Frequently Asked Questions