Decisions and loops
Conditional statements choose which code to run based on a logical condition. Loops repeat operations for different values or subsets of data.
If-then-else
If-then-else decisions are useful if your computation needs different workflows for different variable values.
# Pseudocode
if("<some logical (and boolean) expression>"){
"<do something>"
} else {
"<do something else>"
}
Loops
For loops and while loops are common control structures in many programming languages. In R, the apply family provides functions for repeating operations over data structures.
For loops
Useful if your computation needs the same sequential workflow for different variables or subsets.
# Pseudocode
for(<iteration variable> in <control vector>){
<do something using the iteration variable>
}
Loops with lapply
The apply family is part of R. One frequently used member is lapply(), which calls a function for each input element and returns a list of results. Use it when you want to repeat an operation and collect the results in a list.
# Pseudocode
lapply(<control vector>, function(<iteration variable>){
<do something using the iteration variable>
return(<result>)
})
While loops
Useful when an operation must be repeated while a condition remains true and the number of iterations may not be known in advance.
# Pseudocode
while("<some logical expression with iteration variable>"){
<do something using iteration variable and update it>
}
What loop to choose?
As a general guide, use a for loop to iterate over a known sequence. Use a while loop when repetition depends on a condition checked during execution.
Let’s go through a few examples of each:
- Use a for loop to iterate over a known sequence of values.
- Use a for loop when you know the loop should execute n times.
- Use a while loop when reading records until the end of a file.
- Use a while loop when asking for user input until it meets a condition.
- Use a while loop when the next step depends on the current result.