Switch-style recoding of values

switch_case(
  x,
  ...,
  .preserve = FALSE,
  .default = NA,
  .exhaustive = FALSE,
  preserve = deprecated(),
  default = deprecated()
)

fn_switch_case(
  x,
  fn,
  ...,
  .preserve = FALSE,
  .default = NA,
  .exhaustive = FALSE,
  preserve = deprecated(),
  default = deprecated()
)

Arguments

x

A vector

...

<dynamic-dots> A sequence of two-sided formulas or named arguments.

  • Formulas: Elements of x that match the left hand side (LHS) of formulas will be replaced with the value in the right hand side (RHS). The LHS must evaluate to an atomic vector. The RHS must be of length one. NULL inputs are ignored.

  • Named arguments: for fn_switch_case(), named arguments are passed to the function fn. For switch_case(), named arguments will raise an error.

.preserve

If TRUE, unmatched elements of x will be returned unmodified. (The elements may have their type coerced to be compatible with replacement values.) If FALSE, unmatched elements of x will be replaced with .default. Defaults to FALSE.

.default

If .preserve is FALSE, a value to replace unmatched elements of x. Defaults to NA.

.exhaustive

If TRUE, unmatched elements of x will result in an error. This can be useful to ensure you aren't accidentally forgetting to recode any values. Defaults to FALSE.

Note that if .preserve is TRUE, .exhaustive will never have any effect.

preserve, default

[Deprecated] Deprecated in favor of .preserve and .default

fn

A function to apply to the left-hand side of each formula in ...

Value

A vector of the same length as x.

See also

switch_case_fct() and fn_switch_case_fct() to return a factor and switch_case_list() and fn_switch_case_list() to return a list

grep_case() to recode values with string pattern matching

fn_case(), which applies a function to both x and each formula's LHS

in_case(), a pipeable alternative to dplyr::case_when()

switch() and %in%, which inspired this function

Examples

parties <- sample(c("d", "r", "i", "g", "l"), 20, replace = TRUE)

switch_case(
  parties,
  "d" ~ "Democrat",
  "r" ~ "Republican",
  "i" ~ "Independent",
  "g" ~ "Green",
  "l" ~ "Libertarian"
)
#>  [1] "Libertarian" "Republican"  "Democrat"    "Green"       "Democrat"   
#>  [6] "Republican"  "Independent" "Democrat"    "Democrat"    "Republican" 
#> [11] "Libertarian" "Democrat"    "Green"       "Independent" "Green"      
#> [16] "Green"       "Independent" "Republican"  "Independent" "Republican" 

parties %>%
  switch_case(
    "d" ~ "Democrat",
    "r" ~ "Republican",
    "i" ~ "Independent",
    "g" ~ "Green",
    "l" ~ "Libertarian"
  )
#>  [1] "Libertarian" "Republican"  "Democrat"    "Green"       "Democrat"   
#>  [6] "Republican"  "Independent" "Democrat"    "Democrat"    "Republican" 
#> [11] "Libertarian" "Democrat"    "Green"       "Independent" "Green"      
#> [16] "Green"       "Independent" "Republican"  "Independent" "Republican" 

parties %>%
  switch_case(
    "d" ~ "Democrat",
    "r" ~ "Republican",
    c("i", "g", "l") ~ "Other"
  )
#>  [1] "Other"      "Republican" "Democrat"   "Other"      "Democrat"  
#>  [6] "Republican" "Other"      "Democrat"   "Democrat"   "Republican"
#> [11] "Other"      "Democrat"   "Other"      "Other"      "Other"     
#> [16] "Other"      "Other"      "Republican" "Other"      "Republican"

parties %>%
  switch_case(
    "d" ~ "Democrat",
    "r" ~ "Republican",
    .default = "Other"
  )
#>  [1] "Other"      "Republican" "Democrat"   "Other"      "Democrat"  
#>  [6] "Republican" "Other"      "Democrat"   "Democrat"   "Republican"
#> [11] "Other"      "Democrat"   "Other"      "Other"      "Other"     
#> [16] "Other"      "Other"      "Republican" "Other"      "Republican"

parties %>%
  switch_case(
    "d" ~ "Democrat",
    "r" ~ "Republican",
    .preserve = FALSE
  )
#>  [1] NA           "Republican" "Democrat"   NA           "Democrat"  
#>  [6] "Republican" NA           "Democrat"   "Democrat"   "Republican"
#> [11] NA           "Democrat"   NA           NA           NA          
#> [16] NA           NA           "Republican" NA           "Republican"

parties %>%
  switch_case(
    "d" ~ "Democrat",
    "r" ~ "Republican",
    .preserve = TRUE
  )
#>  [1] "l"          "Republican" "Democrat"   "g"          "Democrat"  
#>  [6] "Republican" "i"          "Democrat"   "Democrat"   "Republican"
#> [11] "l"          "Democrat"   "g"          "i"          "g"         
#> [16] "g"          "i"          "Republican" "i"          "Republican"

data <- c(1, 4, 8, 12, 999, 6, 2, 888, 4, 6, 777)

fn_switch_case(
  data,
  function(x) paste(rep(x, 3), collapse = ""),
  7 ~ "Not asked",
  8 ~ "Refused",
  9 ~ "Missing",
  .preserve = TRUE
)
#>  [1] "1"         "4"         "8"         "12"        "Missing"   "6"        
#>  [7] "2"         "Refused"   "4"         "6"         "Not asked"