In R, decision-making structures allow you to execute certain blocks of code based on the outcome of logical conditions. These conditional statements let your program react to different situations, which is essential for controlling the flow of your code.
This tutorial will cover the following decision-making constructs in R:
Each of these decision-making structures is useful in different scenarios. Let’s explore each with explanations and examples.
1. if Statement
The if statement is used to execute a block of code only if a specified condition is TRUE.
Syntax:
if (condition) { # code to be executed if condition is TRUE }
Example:
# Define a variable x <- 10 # If x is greater than 5, print a message if (x > 5) { print("x is greater than 5") }
- In this example, since x = 10 and the condition x > 5 is TRUE, the code inside the if block is executed, printing:
“x is greater than 5”.
2. if…else Statement
The if…else statement allows you to execute one block of code if the condition is TRUE, and another block of code if the condition is FALSE.
Syntax:
if (condition) { # code to be executed if condition is TRUE } else { # code to be executed if condition is FALSE }
Example:
# Define a variable x <- 3 # Check if x is greater than 5 if (x > 5) { print("x is greater than 5") } else { print("x is less than or equal to 5") }
- In this case, since x = 3, the condition x > 5 is FALSE, so the else block is executed, printing:
“x is less than or equal to 5”.
3. ifelse() Function
The ifelse() function in R is a vectorized version of the if…else statement. It allows you to apply conditional checks on vectors and return different values based on the result of the condition.
Syntax:
ifelse(condition, value_if_TRUE, value_if_FALSE)
Example:
# Define a numeric vector vec <- c(2, 7, 4, 9, 1) # Use ifelse to check if each element is greater than 5 result <- ifelse(vec > 5, "Greater than 5", "Not greater than 5") print(result)
- In this example, the ifelse() function applies the condition vec > 5 to each element in the vector:
- Elements greater than 5 will return “Greater than 5”.
- Elements less than or equal to 5 will return “Not greater than 5”.
- The result is:
[1] "Not greater than 5" "Greater than 5" "Not greater than 5" "Greater than 5" "Not greater than 5"
Example with Arithmetic:
# Define a numeric vector numbers <- c(1, 2, 3, 4, 5) # Use ifelse to apply a conditional operation result <- ifelse(numbers %% 2 == 0, numbers * 2, numbers / 2) print(result)
- In this case:
- If a number is even (i.e., divisible by 2), it is doubled.
- If a number is odd, it is halved.
- The result is:
[1] 0.5 4.0 1.5 8.0 2.5
4. else if Ladder
The else if ladder allows you to check multiple conditions in sequence. Once a condition is met, the corresponding block of code is executed, and the rest of the ladder is skipped.
Syntax:
if (condition1) { # code to be executed if condition1 is TRUE } else if (condition2) { # code to be executed if condition2 is TRUE } else { # code to be executed if both conditions are FALSE }
Example:
# Define a variable x <- 15 # Check multiple conditions using else if ladder if (x > 20) { print("x is greater than 20") } else if (x > 10) { print("x is greater than 10 but less than or equal to 20") } else { print("x is less than or equal to 10") }
- Since x = 15, the second condition x > 10 is TRUE, so the corresponding block is executed, printing:
“x is greater than 10 but less than or equal to 20”.
5. switch() Statement
The switch() statement in R is used to evaluate a single expression and execute one of several code blocks based on the value of the expression. It can be used with both numeric and character values.
Syntax:
switch(expression, case1 = value1, case2 = value2, ...)
Example (Numeric switch()):
# Define a variable with a numeric value x <- 2 # Use switch to choose based on numeric value result <- switch(x, "Case 1", "Case 2", "Case 3" ) print(result)
- In this example, since x = 2, the second case is selected, and the result is:
“Case 2”.
Example (Character switch()):
# Define a variable with a character value operation <- "subtract" # Use switch to execute based on the operation result <- switch(operation, "add" = 10 + 5, "subtract" = 10 - 5, "multiply" = 10 * 5, "divide" = 10 / 5 ) print(result)
- In this example, since the value of operation is “subtract”, the corresponding block is executed, and the result is:
5.
Example with Default Case:
# Define an operation that doesn't exist in the switch cases operation <- "modulus" # Use switch with default case handling result <- switch(operation, "add" = 10 + 5, "subtract" = 10 - 5, "multiply" = 10 * 5, "divide" = 10 / 5, "Invalid operation" ) print(result)
- Since “modulus” is not listed in the cases, the default value “Invalid operation” is returned.
Summary of R Decision-Making Constructs
Statement | Description |
---|---|
if | Executes code if a condition is TRUE. |
if…else | Executes one block of code if the condition is TRUE, otherwise another block. |
ifelse() | Vectorized version of if…else for element-wise conditions. |
else if | Checks multiple conditions in sequence. |
switch() | Executes a block of code based on the value of an expression. |
Conclusion
R provides several decision-making structures that allow you to control the flow of your code based on conditions.
By mastering these constructs, you can write more complex and functional R programs that react dynamically to different inputs and scenarios.
Whether you’re working with simple conditions or multiple branching scenarios, these structures are essential for data analysis and manipulation in R.