Introduction to Loops in R

What are Loops?

Loops are a fundamental concept in programming that allow you to repeat a set of instructions for a specified number of times or until a certain condition is met. In this notebook, we will explore the different types of loops in R and provide exercises to help you practice.

In R, there are three main types of loops: 1. For loops 2. While loops 3. Repeat loops

Let’s explore each type in detail.

For Loops

A for loop is used when you know in advance how many times you want to execute a block of code. It iterates over a sequence of elements, performing the specified actions for each element.

Syntax:

for (variable in sequence) {
  # Code to be repeated
}

Example 1: Printing numbers

for (i in 1:5) {
  print(i)
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Example 2: Iterating over a vector

fruits <- c("apple", "banana", "cherry")
for (fruit in fruits) {
  print(paste("I like", fruit))
}

Output:

[1] "I like apple"
[1] "I like banana"
[1] "I like cherry"

While Loops

A while loop repeats a block of code as long as a specified condition is true. It’s useful when you don’t know in advance how many iterations you need.

Syntax:

while (condition) {
  # Code to be repeated
}

Example: Counting to 5

count <- 1
while (count <= 5) {
  print(count)
  count <- count + 1
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Repeat Loops

A repeat loop is used to execute a block of code indefinitely until a stop condition is met. You must explicitly break out of the loop using a break statement.

Syntax:

repeat {
  # Code to be repeated
  if (stop_condition) {
    break
  }
}

Example: Guessing game

target <- sample(1:10, 1)
repeat {
  guess <- as.integer(readline("Guess a number between 1 and 10: "))
  if (guess == target) {
    print("Correct! You win!")
    break
  } else {
    print("Wrong guess. Try again.")
  }
}

Nested Loops

Loops can be nested inside other loops. This is useful for working with multi-dimensional data or performing complex iterations.

Example: Multiplication table

for (i in 1:5) {
  for (j in 1:5) {
    print(paste(i, "x", j, "=", i*j))
  }
  print("---")
}

Loop Control Statements

R provides two main loop control statements:

  1. break: Exits the loop immediately
  2. next: Skips the rest of the current iteration and moves to the next one

Example: Using break and next

for (i in 1:10) {
  if (i == 3) {
    next  # Skip iteration when i is 3
  }
  if (i == 7) {
    break  # Exit loop when i is 7
  }
  print(i)
}

Output:

[1] 1
[1] 2
[1] 4
[1] 5
[1] 6

Vectorization: An Alternative to Loops

While loops are powerful, R is designed for vectorized operations, which are often faster and more concise. Many tasks that require loops in other languages can be accomplished using vectorized functions in R.

Example: Summing elements

Using a loop:

numbers <- 1:100
sum <- 0
for (num in numbers) {
  sum <- sum + num
}
print(sum)

Using vectorization:

numbers <- 1:100
sum <- sum(numbers)
print(sum)

Both produce the same result, but the vectorized version is faster and more concise.

Exercises

Now, let’s practice with some exercises. Try to solve these on your own before looking at the solutions.

  1. Write a for loop that prints the squares of numbers from 1 to 10.

  2. Create a while loop that prints even numbers from 2 to 20.

  3. Use a repeat loop to implement a simple calculator that keeps asking for operations until the user chooses to quit.

  4. Write nested for loops to create a 5x5 identity matrix (1s on the diagonal, 0s elsewhere).

  5. Create a for loop that iterates over a vector of numbers and uses next to skip negative numbers, printing only positive ones.

  6. Write a function that uses a while loop to calculate the factorial of a given number.

  7. Implement the FizzBuzz game using a for loop: For numbers 1 to 100, print “Fizz” if the number is divisible by 3, “Buzz” if it’s divisible by 5, “FizzBuzz” if it’s divisible by both, and the number itself otherwise.

  8. Create a loop that simulates rolling a die until a 6 is rolled, counting the number of rolls.

Solutions

Click to reveal solutions
  1. Squares of numbers from 1 to 10:
for (i in 1:10) {
  print(paste(i, "squared is", i^2))
}
  1. Even numbers from 2 to 20:
num <- 2
while (num <= 20) {
  print(num)
  num <- num + 2
}
  1. Simple calculator with repeat loop:
repeat {
  operation <- readline("Enter operation (+, -, *, /) or 'q' to quit: ")
  if (operation == 'q') {
    break
  }
  num1 <- as.numeric(readline("Enter first number: "))
  num2 <- as.numeric(readline("Enter second number: "))
  result <- switch(operation,
                   "+" = num1 + num2,
                   "-" = num1 - num2,
                   "*" = num1 * num2,
                   "/" = num1 / num2,
                   "Invalid operation")
  print(paste("Result:", result))
}
  1. 5x5 identity matrix:
identity_matrix <- matrix(0, nrow = 5, ncol = 5)
for (i in 1:5) {
  for (j in 1:5) {
    if (i == j) {
      identity_matrix[i, j] <- 1
    }
  }
}
print(identity_matrix)
  1. Printing only positive numbers:
numbers <- c(-2, 5, 0, -1, 3, -4, 7)
for (num in numbers) {
  if (num <= 0) {
    next
  }
  print(num)
}
  1. Factorial function using while loop:
factorial <- function(n) {
  result <- 1
  while (n > 1) {
    result <- result * n
    n <- n - 1
  }
  return(result)
}
print(factorial(5))  # Should print 120
  1. FizzBuzz game:
for (i in 1:100) {
  if (i %% 3 == 0 && i %% 5 == 0) {
    print("FizzBuzz")
  } else if (i %% 3 == 0) {
    print("Fizz")
  } else if (i %% 5 == 0) {
    print("Buzz")
  } else {
    print(i)
  }
}
  1. Die rolling simulation:
rolls <- 0
repeat {
  roll <- sample(1:6, 1)
  rolls <- rolls + 1
  if (roll == 6) {
    break
  }
}
print(paste("It took", rolls, "rolls to get a 6"))

Remember, practice is key to mastering loops in R. Try to come up with your own examples and challenges to further your understanding!

LS0tCnRpdGxlOiAiTG9vcHMgaW4gUiIKb3V0cHV0OiBodG1sX25vdGVib29rCmF1dGhvcjogRHIuIE5heWVsIEJldHRhY2hlCi0tLQojIEludHJvZHVjdGlvbiB0byBMb29wcyBpbiBSCgojIyBUYWJsZSBvZiBDb250ZW50cwoxLiBbV2hhdCBhcmUgTG9vcHM/XSgjd2hhdC1hcmUtbG9vcHMpCjIuIFtGb3IgTG9vcHNdKCNmb3ItbG9vcHMpCjMuIFtXaGlsZSBMb29wc10oI3doaWxlLWxvb3BzKQo0LiBbUmVwZWF0IExvb3BzXSgjcmVwZWF0LWxvb3BzKQo1LiBbTmVzdGVkIExvb3BzXSgjbmVzdGVkLWxvb3BzKQo2LiBbTG9vcCBDb250cm9sIFN0YXRlbWVudHNdKCNsb29wLWNvbnRyb2wtc3RhdGVtZW50cykKNy4gW1ZlY3Rvcml6YXRpb246IEFuIEFsdGVybmF0aXZlIHRvIExvb3BzXSgjdmVjdG9yaXphdGlvbi1hbi1hbHRlcm5hdGl2ZS10by1sb29wcykKOC4gW0V4ZXJjaXNlc10oI2V4ZXJjaXNlcykKCiMjIFdoYXQgYXJl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