The R switch()
Function
Learn how to route your code down different paths based on a single value: cleaner, faster, and more readable than chained if/else blocks.
What Is switch()?
switch() is a built-in R function that takes a single expression (usually a character string) and compares it against a list of named cases. When it finds a match, it returns the value associated with that case.
Instead of writing long chains of if ... else if ... else, switch() lets you handle multiple known options in a single, compact expression.
Why Use switch()?
The Routing Metaphor
The value of the expression (here, a color name) determines which branch runs. Only the matching case returns its result: all others are skipped.
Core Syntax
"case1" = result1,
"case2" = result2,
"case3" = result3,
default_result # optional fallback
)
How R Evaluates switch()
switch() vs if/else
| switch() | if/else chain | |
|---|---|---|
| Best for | One value, many known options | Complex logical conditions |
| Input type | Character string (or integer) | Any logical expression |
| Readability | High: flat list of cases | Moderate: nested blocks |
| Adding cases | Add one argument | Add another else if block |
| Default | Unnamed last argument | Final else block |
| Range checks | Not supported | Supported |
How to Apply switch()
Example 1: Day Type
day_type <- switch(day,
"Monday" = "Weekday",
"Tuesday" = "Weekday",
"Wednesday" = "Weekday",
"Thursday" = "Weekday",
"Friday" = "Weekday",
"Saturday" = "Weekend",
"Sunday" = "Weekend"
)
Example 2: Fall-Through Cases
When multiple cases should return the same value, leave the earlier ones empty: R falls through to the next non-empty result.
day_type <- switch(day,
"Monday" = ,
"Tuesday" = ,
"Wednesday" = ,
"Thursday" = ,
"Friday" = "Weekday",
"Saturday" = ,
"Sunday" = "Weekend"
)
Example 3: Using a Default
price <- switch(fruit,
"apple" = 1.20,
"banana" = 0.50,
"cherry" = 3.00,
0.00 # default: unknown fruit
)
Example 4: switch() Inside a Function
switch(op,
"add" = a + b,
"subtract" = a - b,
"multiply" = a * b,
"divide" = a / b,
stop("Unknown operation: ", op)
)
}
Anatomy of a switch() Call
Key Rules to Remember
Common Mistakes
Summary Cheat Sheet
| Feature | Details |
|---|---|
| Purpose | Route code by matching one value to named cases |
| Syntax | switch(EXPR, "case" = value, ...) |
| Input type | Character string (preferred) or integer |
| Match type | Exact match only (case-sensitive) |
| Fall-through | Only with empty cases (name = ,) |
| Default | Unnamed last argument (or use stop()) |
| No match + no default | Returns NULL (invisible) |
Question 1 of 5
What does switch() do when the input expression does not match any named case and no default is provided?
Question 2 of 5
Code:
result <- switch("b",
"a" = 10,
"b" = ,
"c" = 30,
99
)
What value is stored in result?
Question 3 of 5
Which of the following is NOT a good use case for switch()?
Question 4 of 5
In R's switch(), how do you make multiple case names return the same value?
Question 5 of 5
Code:
get_sound <- function(animal) {
switch(animal,
"cat" = "meow",
"dog" = "woof",
"cow" = "moo",
"unknown sound"
)
}
What does get_sound("Cat") return?