R for Loop

Introduction

In this chapter, you will learn about the for loop in R. The for loop is a fundamental control structure that allows you to iterate over a sequence of elements and execute a block of code for each element. Understanding how to use the for loop is essential for performing repetitive tasks and automating processes in your R programs.

The for Loop

Basic Usage

The for loop in R iterates over a sequence (such as a vector, list, or other iterable objects) and executes a block of code for each element in the sequence.

Syntax

for (variable in sequence) {
  # Code to execute for each element in the sequence
}
  • variable: A variable that takes the value of each element in the sequence.
  • sequence: A sequence of elements to iterate over.

Example 1: Iterating Over a Vector

In this example, the for loop iterates over a vector and prints each element.

Example:

# Iterating over a vector
numbers <- c(1, 2, 3, 4, 5)

for (num in numbers) {
  print(num)
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5

Example 2: Using a for Loop with a Sequence

In this example, the for loop iterates over a sequence of numbers generated by the 1:10 expression.

Example:

# Using a for loop with a sequence
for (i in 1:10) {
  print(i)
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9
# [1] 10

Example 3: Iterating Over a List

In this example, the for loop iterates over a list and prints each element.

Example:

# Iterating over a list
my_list <- list("apple", "banana", "cherry")

for (fruit in my_list) {
  print(fruit)
}
# Output:
# [1] "apple"
# [1] "banana"
# [1] "cherry"

Example 4: Nested for Loops

In this example, nested for loops are used to iterate over a matrix and print each element.

Example:

# Nested for loops
matrix_data <- matrix(1:9, nrow = 3, ncol = 3)

for (i in 1:nrow(matrix_data)) {
  for (j in 1:ncol(matrix_data)) {
    print(matrix_data[i, j])
  }
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9

Example 5: Using the break Statement

In this example, the break statement is used to exit the loop when a condition is met.

Example:

# Using the break statement
for (i in 1:10) {
  if (i == 5) {
    break  # Exit the loop
  }
  print(i)
}
# Output:
# [1] 1
# [1] 2
# [1] 3
# [1] 4

Example 6: Using the next Statement

In this example, the next statement is used to skip the current iteration when a condition is met.

Example:

# Using the next statement
for (i in 1:10) {
  if (i %% 2 == 0) {
    next  # Skip even numbers
  }
  print(i)
}
# Output:
# [1] 1
# [1] 3
# [1] 5
# [1] 7
# [1] 9

Example Program with for Loop

Here is an example program that demonstrates the use of the for loop in R:

# R Program to Demonstrate for Loop

# Declare a vector of numbers
numbers <- c(10, 20, 30, 40, 50)

# Calculate the sum of the numbers using a for loop
sum <- 0
for (num in numbers) {
  sum <- sum + num
}

# Print the sum
print(paste("The sum of the numbers is:", sum))
# Output: [1] "The sum of the numbers is: 150"

# Using a for loop to create a multiplication table
multiplication_table <- function(n) {
  for (i in 1:10) {
    result <- n * i
    print(paste(n, "x", i, "=", result))
  }
}

# Create a multiplication table for 5
multiplication_table(5)
# Output:
# [1] "5 x 1 = 5"
# [1] "5 x 2 = 10"
# [1] "5 x 3 = 15"
# [1] "5 x 4 = 20"
# [1] "5 x 5 = 25"
# [1] "5 x 6 = 30"
# [1] "5 x 7 = 35"
# [1] "5 x 8 = 40"
# [1] "5 x 9 = 45"
# [1] "5 x 10 = 50"

Conclusion

In this chapter, you learned about the for loop in R, including how to iterate over vectors, lists, sequences, and matrices. You also learned how to use nested for loops, and how to incorporate the break and next statements within a for loop. The for loop is used for performing repetitive tasks and automating processes in your R programs. By mastering the for loop, you can write more efficient and flexible R code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top