switch()
switch(EXPR, ...) The switch() function selects and evaluates one alternative from a list of arguments based on the value of EXPR. It handles two distinct modes: integer indexing and character string matching. This makes it useful for dispatching behavior based on a condition without writing lengthy if-else chains.
Syntax
switch(EXPR, ...)
The first argument, EXPR, is always evaluated as the selector. When EXPR is an integer, switch() returns the nth element in .... When EXPR is a character string, it matches the string exactly to named elements in ....
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
EXPR | integer or character | Required | The expression determining which alternative is selected. If named, the name must partially match EXPR. |
... | arguments | Required | The list of alternatives. Named elements are matched by character EXPR; an unnamed element serves as the default. |
Return Value
The value of the selected element. Returns NULL invisibly when no element is selected (e.g., integer out of bounds or character with no match and no default).
Examples
Integer index mode
When EXPR evaluates to an integer, switch() returns the element at that position (1-indexed).
# First element
switch(1, "apple", "banana", "cherry")
# [1] "apple"
# Second element
switch(2, "apple", "banana", "cherry")
# [1] "banana"
# Third element
switch(3, "apple", "banana", "cherry")
# [1] "cherry"
# Out of bounds — returns NULL silently
switch(5, "apple", "banana", "cherry")
# [1] NULL
Character string mode
When EXPR is a character string, switch() matches it exactly to the names of the elements in ....
# Named matching
switch("red",
red = "apple",
green = "banana",
blue = "cherry"
)
# [1] "apple"
# No match — falls through to unnamed default
switch("purple",
red = "apple",
green = "banana",
blue = "cherry",
"unknown fruit"
)
# [1] "unknown fruit"
# No match and no default
switch("purple",
red = "apple",
green = "banana",
blue = "cherry"
)
# [1] NULL
Missing named element fallback
If a named element is missing, switch() evaluates the next non-missing element. This lets you create optional handlers with fallthrough behavior.
switch("cc",
a = 1,
cc =, # missing — falls through
cd = 2, # this gets evaluated
d = 3
)
# [1] 2
Practical dispatch pattern
switch() is commonly used to dispatch behavior in functions based on a configuration argument.
calculate_stats <- function(data, method = "mean") {
switch(method,
mean = mean(data),
median = median(data),
sd = sd(data),
var = var(data),
stop("Unknown method: ", method)
)
}
calculate_stats(c(1, 2, 3, 4, 5), method = "median")
# [1] 3
calculate_stats(c(1, 2, 3, 4, 5), method = "sd")
# [1] 1.58114
Default value pattern
An unnamed argument at the end serves as the default when no named element matches the character EXPR.
get_status_message <- function(code) {
switch(code,
success = "Operation completed successfully.",
error = "An error occurred.",
warning = "Something looks off.",
"Unknown status code" # default
)
}
get_status_message("success")
# [1] "Operation completed successfully."
get_status_message("timeout")
# [1] "Unknown status code"
Edge Cases
Invalid EXPR values
Integer 0 causes an error. NULL returns NULL. NA_character_ returns NULL since NA never matches.
# Zero is invalid
switch(0, "a", "b", "c")
# Error in switch(0, "a", "b", "c") :
# switching argument must be non-zero
# NULL input
switch(NULL, a = 1, b = 2)
# [1] NULL
# NA_character_ input
switch(NA_character_, a = 1, b = 2)
# [1] NULL
Multiple unnamed elements
More than one unnamed element causes an error since switch() cannot determine which to use as the default.
switch("x",
a = 1,
"default1",
"default2"
)
# Error in switch("x", a = 1, "default1", "default2") :
# more than one unnamed argument in switch
Multiple character matches
If two named elements partially match the same EXPR, the first match wins.
switch("cc",
c = "short c",
cc = "double c"
)
# [1] "short c"
Common Mistakes
Assuming out-of-bounds returns a warning or error. When using integer mode with an index larger than the number of alternatives, switch() silently returns NULL with no warning. Always validate the index range if out-of-bounds behavior matters.
Using 1-indexed thinking in character mode. Character matching requires exact name matches, not partial index lookups. switch("1", "1" = "first") works but switch(1, "first") uses integer mode.
Forgetting the unnamed default for character EXPR. If you pass a character string that might not match any named element, add an unnamed argument at the end to serve as the default, otherwise you’ll get a silent NULL.