Control Statements: If - Conditional Block
Why Learn Control Statements in Data Analytics?
Imagine you are developing a credit approval system for a bank. You have a dataset of customer credit scores. You need to categorize each applicant automatically based on their score:
- Score of 700 or above: Status is
"Approved". - Score between 600 and 699: Status is
"Under Review". - Score below 600: Status is
"Denied".
If you write a simple sequential script, it will execute every line of code without stopping to think. To make decisions, you must deviate from this straight path. You need Control Statements (if, else if, and else) to guide the program down different paths depending on each applicant's credit score.
1. Relational Operators in R
R uses relational operators to compare values. Every comparison returns a logical value: TRUE or FALSE.
| Operator Name | Notation | Example | Result |
|---|---|---|---|
| Greater than | > |
9 > 8 |
TRUE |
| Less than | < |
9 < 8 |
FALSE |
| Greater than or equal to | >= |
9 >= 8 |
TRUE |
| Less than or equal to | <= |
9 <= 8 |
FALSE |
| Equal to | == |
9 == 8 |
FALSE |
| Not equal to | != |
9 != 8 |
TRUE |
# Testing comparisons
age <- 18
is_adult <- age >= 18
print(is_adult) # TRUE
2. Logical Operators in R
R has two types of logical operators: short-circuiting (used in if conditions) and element-wise (used for data filtering, which we will learn later).
For if statements, always use the short-circuiting double operators:
| Operator Name | Notation | Example | Result |
|---|---|---|---|
| Logical AND | && |
9 > 8 && 5 > 4 |
TRUE (both must be TRUE) |
| Logical OR | ` | ` | |
| Logical NOT | ! |
!(9 > 8) |
FALSE (reverses the value) |
Short-Circuit Evaluation
&&stops at the firstFALSE: If the left side isFALSE, R knows the entire expression must beFALSEand skips evaluating the right side.||stops at the firstTRUE: If the left side isTRUE, R knows the entire expression must beTRUEand skips evaluating the right side.
3. The if Statement Syntax
In R, conditions must be enclosed in parentheses ( ), and the block of code to run must be enclosed in curly braces { }.
score <- 750
if (score >= 700) {
print("Status: Approved")
}
4. Multi-Way Conditionals: else if and else
To handle multiple branches, chain them together using else if and else:
score <- 650
if (score >= 700) {
print("Status: Approved")
} else if (score >= 600) {
print("Status: Under Review")
} else {
print("Status: Denied")
}
In R, the else or else if keyword must be on the same line as the closing curly brace } of the preceding block.
Incorrect:
if (score >= 700) {
print("Approved")
}
else { # This will throw a syntax error in R!
print("Denied")
}
5. Inline Conditionals: ifelse() vs. if_else()
For simple conditional assignments, writing a full multi-line if/else block can be verbose. R offers vectorized inline functions for this.
Base R: ifelse()
The built-in ifelse(test, yes, no) evaluates a test condition, returning the yes value if TRUE and the no value if FALSE:
score <- 550
status <- ifelse(score >= 600, "Pass", "Fail")
print(status) # "Fail"
Base R's ifelse() is flexible and will automatically coerce data types if they differ between the yes and no values (e.g., mixing characters and numbers).
Tidyverse: if_else()
The dplyr package (part of the tidyverse) provides a stricter alternative called if_else(condition, true, false, missing = NULL):
library(dplyr)
score <- 650
# Returns "large" if >600, else "small"
status <- if_else(score > 600, "large", "small")
print(status)
Unlike base ifelse(), the true and false arguments of dplyr::if_else() must return the exact same data type. If you try to mix types (e.g., if_else(x > 3, "large", 0)), R will throw a compilation error.
Furthermore, if_else() has a fourth argument, missing, which specifies what value to return if the condition evaluates to NA. This prevents unexpected propagation of missing values!
Hands-on Exercises
Exercise 1: Risk Profiling
In investment analysis, a client's risk tolerance is determined by their age. Write R code to:
- Assign
age <- 62. - Write an
if/else if/elsestructure:- If age is under 35, print
"Risk Profile: Aggressive". - If age is between 35 and 60, print
"Risk Profile: Moderate". - If age is above 60, print
"Risk Profile: Conservative".
- If age is under 35, print
# Write your code below and click Run Code
Click to view Answer
age <- 62
if (age < 35) {
print("Risk Profile: Aggressive")
} else if (age <= 60) {
print("Risk Profile: Moderate")
} else {
print("Risk Profile: Conservative")
}
Exercise 2: Premium Pricing Logic
A subscription service charges $15 for normal users, but offers a discount based on two criteria:
- If the user is a student OR is under 18, the subscription price is $10.
- Otherwise, the price is $15.
Write R code to:
- Set
is_student <- TRUEandage <- 22. - Calculate the price using
if/elsewith logical operators. - Print the final price.
# Write your code below and click Run Code
Click to view Answer
is_student <- TRUE
age <- 22
if (is_student || age < 18) {
price <- 10
} else {
price <- 15
}
print(paste("Subscription Price: $", price))