Present since hutils 1.2.0. Vectorized version of switch. Used to avoid or make clearer the result of if_else(Expr == , ..1, if_else(Expr == , ..2, ...))

Switch(Expr, ..., DEFAULT, IF_NA = NULL, MUST_MATCH = FALSE)

Arguments

Expr

A character vector.

...

As in switch, a list of named alternatives. Unlike switch, unnamed vectors are taken to match "". Likewise, NA values in Expr must be assigned via IF_NA.

DEFAULT

A mandatory default value should any name of ... be left unmatched.

IF_NA

Optional value to replace missing (NA_character_) values in Expr.

MUST_MATCH

(logical, default: FALSE) Must every value in Expr be matched by a conversion in ...? If TRUE any output equal to the value of DEFAULT is an error.

Value

For every element of ... whose name matches an element of Expr, that element's value.

Examples

Switch(c("a", "b", "c", "a"), "a" = 1, "b" = 2, "c" = 3, "4" = 4, DEFAULT = 0)
#> [1] 1 2 3 1