Control Flow and Loops in R

Practical Guide to Conditionals and Loop Constructs in R

Learn how to implement control flow in R using conditionals and loops. This tutorial covers if-else statements, for loops, while loops, and repeat loops with practical examples to help you build dynamic R scripts.

Programming
Author
Affiliation
Published

February 10, 2024

Modified

March 11, 2025

Keywords

R control flow, R loops, if-else in R, for loop in R, while loop in R

Introduction

Control flow is a fundamental concept in programming that allows you to direct the execution of your code based on conditions. In R, mastering conditionals and loop constructs is essential for writing dynamic and efficient scripts. In this tutorial, you’ll learn how to use if-else statements for decision-making and various loops (for, while, and repeat) to perform repetitive tasks.



Conditional Statements

Conditional statements enable your code to execute different blocks of code depending on whether a condition is TRUE or FALSE.

Using if and if-else

# Example using if and if-else
x <- 10

if (x > 5) {
  print("x is greater than 5")
} else {
  print("x is not greater than 5")
}
[1] "x is greater than 5"

Using if, else if, and else

# Determine the category based on value of x
x <- 0

if (x > 0) {
  print("x is positive")
} else if (x < 0) {
  print("x is negative")
} else {
  print("x is zero")
}
[1] "x is zero"

Loop Constructs

Loops allow you to execute a block of code repeatedly. R supports several types of loops, including for loops, while loops, and repeat loops.

For Loops

For loops iterate over a sequence of elements. They are useful for processing each element in a vector or list.

# Print each element of a vector
numbers <- c(1, 2, 3, 4, 5)
for (num in numbers) {
  print(num)
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

While Loops

While loops continue executing as long as a specified condition remains TRUE.

Print numbers from 1 to 5 using a while loop:

counter <- 1
while (counter <= 5) {
  print(counter)
  counter <- counter + 1
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Repeat Loops

Repeat loops execute a block of code indefinitely until a break condition is met. They are useful when the number of iterations is not known in advance.

Using a repeat loop to print numbers until a condition is met:

counter <- 1
repeat {
  print(counter)
  counter <- counter + 1
  if (counter > 5) {
    break
  }
}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Best Practices

  • Clarity:
    Write clear and well-commented code. Use descriptive variable names and avoid overly complex nested conditionals or loops.

  • Avoid Infinite Loops:
    Always ensure that your loop has a well-defined exit condition to prevent infinite loops.

  • Vectorization:
    Remember that R is optimized for vectorized operations. In many cases, you can replace loops with vectorized functions for better performance.

Conclusion

Mastering control flow and loops in R is key to developing dynamic and efficient scripts. By understanding and using conditionals and loop constructs effectively, you can automate repetitive tasks and handle decision-making in your code. Experiment with these examples, and apply best practices to improve the clarity and performance of your R programs.

Further Reading

Happy coding, and enjoy exploring control flow and loops in R!

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2024,
  author = {Kassambara, Alboukadel},
  title = {Control {Flow} and {Loops} in {R}},
  date = {2024-02-10},
  url = {https://www.datanovia.com/learn/programming/r/basics/control-flow-and-loops.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2024. “Control Flow and Loops in R.” February 10, 2024. https://www.datanovia.com/learn/programming/r/basics/control-flow-and-loops.html.