Routine data analysis tasks in RStudio, such as cleaning, filtering, or summarizing data, are typically performed with an automated code that can evaluate logical conditions and repeat certain actions. In this post, we’ll take a closer look at how logical conditions (e.g. if, else, and else if), and loop statements (e.g. for, while, and until) work in RStudio, along with clear examples that you can apply to your own projects.
1. The If, Else, and Else If Statement
The if statement is a conditional statement that allows you to execute a specific block of code only if a certain condition is true. If that condition is false, the else statement can be used to execute an alternative block of code. In cases where there are more than two possible outcomes, the else if statement allows for additional conditions to be evaluated. Let’s have a look at this example:
# Assigning a value to the score variable:
score <- 85
# Determining the letter grade based on the score:
if (score >= 90) {
print("Grade: A")
}
else if (score >= 80) {
print("Grade: B")
}
else {
print("Below B")
}
# Output:
# [1] "Grade: B"
This code checks the value stored in the variable score and prints a corresponding grade based on that value. That is, if the score is 90 or above, the code prints “Grade: A”; if the score is between 80 and 89, the code prints “Grade: B”; otherwise, the code prints “Below B”. In this case, the score is 85, which is between 80 and 90, and therefore the code prints “Grade: B”.
2. The For Loop
A for loop is a tool used to automate a repetitive task by iterating through a collection of items and performing the same set of instructions on each item, one by one, until it reaches the end. For example, the following code creates a vector of test scores and then uses a for loop to print each score in that vector:
# Creating a vector of scores:
scores <- c(88, 92, 79, 85)
# Looping through each score in the vector:
for (s in scores) {
# Printing the current score:
print(s)
}
# Output:
# [1] 88
# [1] 92
# [1] 79
# [1] 85
This code loops through each value stored in the scores vector and temporarily assigns that value to the variable s. During each iteration, the print() function displays the current score before moving on to the next value in the vector. Note that in this example, the for loop directly accessed each value in the vector. Sometimes, however, you need to work with the position (index) of each element rather than the value itself, especially when you want to modify the vector. In that case, the loop iterates over the index numbers of the vector instead. The following code uses index-based looping to add 5 points to each test score:
# Creating a vector of scores:
scores <- c(88, 92, 79, 85)
# Looping through the index numbers of the vector:
for (i in 1:length(scores)) {
scores[i] <- scores[i] + 5
}
# Printing the updated vector:
print(scores)
# Output:
# [1] 93 97 84 90
This code uses a for loop to iterate through each element of the scores vector by its index, increasing every individual value by 5 points. It then displays the updated set of scores in the console.
3. The While Loop
A while loop is used when you don't necessarily know how many times a task needs to repeat, but you know exactly what condition must be true for the loop to stop. It continues to execute a block of code as long as a specific logical test remains true. For example, the following code starts at 51 and uses a while loop to increment the number until it finds the first value divisible by 7:
# Setting the value of x to 51:
x <- 51
# Incrementing x by 1 until a number divisible by 7 is found:
while (x %% 7 != 0) {
x <- x + 1
}
# Printing the final result:
print(x)
This code initializes a variable x at 51 and uses a while loop to repeatedly add 1 to it until it finds a number divisible by 7 (where the remainder after division by 7 is zero). Once the condition is satisfied, the loop terminates and prints the result, which is the smallest number greater than 50 that meets the criteria.
4. Combining Loops and Conditionals
In many situations, loops and conditional statements are used together to evaluate each individual element and take different actions depending on the result. For example, the following code iterates through a vector of test scores and uses a conditional statetement to print whether each score is a pass or a fail:
# Creating a vector of test scores:
scores <- c(45, 67, 88, 52, 93)
# Looping through each score in the vector:
for (s in scores) {
# Checking whether the score is a pass or fail:
if (s >= 60) {
print(paste(s, "is a passing score"))
} else {
print(paste(s, "is a failing score"))
}
}
# Output:
# [1] "45 is a failing score"
# [1] "67 is a passing score"
# [1] "88 is a passing score"
# [1] "52 is a failing score"
# [1] "93 is a passing score"
By combining loops and conditional statements, you gain the ability to write scripts that are both flexible and powerful. Instead of manually editing data or running repetitive commands, you can build logic that "thinks" for you — automatically categorizing data points, cleaning inconsistent entries, or performing complex calculations across massive datasets.
5. Common Mistakes to Avoid
- Using
=instead of==insideifconditions - Forgetting to update variables inside a
whileloop (causes infinite loops) - Modifying vectors in a
forloop without using indices (e.g.,scores[i])
Need Help from an R Tutor?
If you’re finding it challenging to apply loops or conditional statements in R, working with an experienced tutor can save you time and make learning R a more enjoyable, less stressful experience. Visit our R Tutor page to learn more about our one-on-one tutoring services and assignment assistance.
