Introduction
In this chapter, you will learn about the switch
statement in R. The switch
statement allows you to execute one of several blocks of code based on the value of a variable or an expression. It provides a cleaner and more readable way to handle multiple conditional branches compared to using multiple if-else if
statements.
The switch Statement
Basic Usage
The switch
statement in R can be used in two main ways:
- When the first argument is a character string, it matches this string against the names of the other arguments.
- When the first argument is an integer, it matches against the positional argument list.
Syntax
Character String Matching:
switch(expression,
case1 = {code1},
case2 = {code2},
caseN = {codeN},
default = {default_code})
Positional Matching:
switch(integer_expression,
case1,
case2,
caseN,
default_code)
Examples
Example 1: Character String Matching
In this example, the switch
statement matches the character string against the names of the cases.
Example:
# Character string matching
fruit <- "apple"
result <- switch(fruit,
apple = "You chose an apple.",
banana = "You chose a banana.",
orange = "You chose an orange.",
"Unknown fruit")
print(result)
# Output: "You chose an apple."
Example 2: Positional Matching
In this example, the switch
statement matches the integer value against the position of the cases.
Example:
# Positional matching
choice <- 2
result <- switch(choice,
"First choice",
"Second choice",
"Third choice",
"Unknown choice")
print(result)
# Output: "Second choice"
Example 3: Using Expressions in switch
You can also use expressions within the switch
cases.
Example:
# Using expressions in switch
operation <- "multiply"
a <- 5
b <- 3
result <- switch(operation,
add = a + b,
subtract = a - b,
multiply = a * b,
divide = a / b,
"Unknown operation")
print(result)
# Output: 15
Example 4: Handling Missing Cases with switch
If a case is not matched, switch
can return a default value if specified.
Example:
# Handling missing cases with switch
day <- "Sunday"
result <- switch(day,
Monday = "Start of the work week.",
Tuesday = "Second day of the work week.",
Wednesday = "Midweek day.",
Thursday = "Almost the weekend.",
Friday = "Last work day.",
"Weekend!")
print(result)
# Output: "Weekend!"
Example Program with switch Statement
Here is an example program that demonstrates the use of the switch
statement in R:
# R Program to Demonstrate switch Statement
# Function to perform an operation based on the choice
performOperation <- function(choice, x, y) {
result <- switch(choice,
add = x + y,
subtract = x - y,
multiply = x * y,
divide = if (y != 0) x / y else "Division by zero",
"Invalid operation")
return(result)
}
# User input
operation <- "divide"
x <- 20
y <- 0
# Perform the chosen operation
result <- performOperation(operation, x, y)
# Print the result
print(paste("Operation:", operation))
print(paste("Result:", result))
# Output:
# [1] "Operation: divide"
# [1] "Result: Division by zero"
Conclusion
In this chapter, you learned about the switch
statement in R, including character string matching, positional matching, using expressions within switch
, and handling missing cases. The switch
statement provides a clean and efficient way to handle multiple conditional branches in your R programs, making your code more readable and easier to maintain. By mastering the switch
statement, you can simplify complex decision-making processes in your code.