These functions are equivalent to in_case(), switch_case(), grep_case(), fn_case(), and fn_switch_case() but return factors with their levels determined by the order of their case statements.

in_case_fct(
  ...,
  .preserve = FALSE,
  .default = NA,
  .ordered = FALSE,
  preserve = deprecated(),
  default = deprecated(),
  ordered = deprecated()
)

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

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

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

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

Arguments

...

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

  • Formulas: Elements of x that return TRUE when passed to fn with the left hand side (LHS) of each formula will be replaced with the value in the right hand side (RHS). The LHS must evaluate to a logical vector when passed to fn with x. The RHS must be of length 1 or the same length as all other RHS.

  • Named arguments: named arguments are passed as additional arguments to the function fn.

.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.

.ordered

A logical. If TRUE, returns an ordered factor. If FALSE, returns an unordered factor.

preserve, default, ordered

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

x

A vector

.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.

fn

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

Either a quoted or unquoted function name, an anonymous function, or a purrr-style formula.

The function should take two inputs, the first being x and the second being the left-hand side of the formula. The function should return a logical vector, either of length 1 or the same length as x.

Value

A factor vector of length 1 or n, matching the length of the logical input or output vectors. Levels are determined by the order of inputs to ... and .default. Inconsistent lengths will generate an error.

The position of the .default argument is taken into account when setting factor levels in *_case_fct() functions. For example, if the .default argument is given before any case statements, the default value will be the first level of the factor; if the .default argument is positioned in between two case statements, the default value will be ordered in between the value of the two case statements.

See also

in_case(), switch_case(), grep_case(), fn_case(), and fn_case_fct() on which these functions are based.

Examples

1:10 %>%
  in_case_fct(
    . %% 2 == 0 ~ "even",
    . %% 2 == 1 ~ "odd"
  )
#>  [1] odd  even odd  even odd  even odd  even odd  even
#> Levels: even odd

switch_case_fct(
  c("a", "b", "c"),
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple"
)
#> [1] apple      banana     cantaloupe
#> Levels: cantaloupe banana apple

switch_case_fct(
  c("a", "b", "c", "d"),
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple"
)
#> [1] apple      banana     cantaloupe <NA>      
#> Levels: cantaloupe banana apple

switch_case_fct(
  c("a", "b", "c", "d"),
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple",
  .preserve = TRUE
)
#> [1] apple      banana     cantaloupe d         
#> Levels: cantaloupe banana apple d

switch_case_fct(
  c("a", "b", "c", "d"),
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple",
  .default = "other"
)
#> [1] apple      banana     cantaloupe other     
#> Levels: cantaloupe banana apple other

switch_case_fct(
  c("a", "b", "c", "d"),
  .default = "other",
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple"
)
#> [1] apple      banana     cantaloupe other     
#> Levels: other cantaloupe banana apple

switch_case_fct(
  c("a", "b", "c", "d"),
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  .default = "other",
  "a" ~ "apple"
)
#> [1] apple      banana     cantaloupe other     
#> Levels: cantaloupe banana other apple

grep_case_fct(
  c("caterpillar", "dogwood", "catastrophe", "dogma"),
  "cat" ~ "feline",
  "dog" ~ "canine"
)
#> [1] feline canine feline canine
#> Levels: feline canine

fn_case_fct(
  c("a", "b", "c"),
  `%in%`,
  "c" ~ "cantaloupe",
  "b" ~ "banana",
  "a" ~ "apple"
)
#> [1] apple      banana     cantaloupe
#> Levels: cantaloupe banana apple