Switch-style recoding of values with string pattern matching
Usage
grep_case(
x,
...,
.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 regex pattern on the left hand side (LHS) of formulas will be replaced with the value in the right hand side (RHS). The LHS must evaluate to a character string. The RHS must be of length one.NULL
inputs are ignored.Named arguments: named arguments are passed to
grepl()
.
- .preserve, preserve
If
TRUE
, unmatched elements ofx
will be returned unmodified. (The elements may have their type coerced to be compatible with replacement values.) IfFALSE
, unmatched elements ofx
will be replaced with.default
. Defaults toFALSE
.- .default, default
If
.preserve
isFALSE
, a value to replace unmatched elements ofx
. Defaults toNA
.- .exhaustive
If
TRUE
, unmatched elements ofx
will result in an error. This can be useful to ensure you aren't accidentally forgetting to recode any values. Defaults toFALSE
.Note that if
.preserve
isTRUE
,.exhaustive
will never have any effect.
See also
grep_case_fct()
to return a factor and
grep_case_list()
to return a list
fn_case()
, to apply a function other than grepl()
to each case
switch_case()
to recode values with exact matching
in_case()
, a pipeable alternative to dplyr::case_when()
Examples
words <- c("caterpillar", "dogwood", "catastrophe", "dogma")
grep_case(
words,
"cat" ~ "feline",
"dog" ~ "canine"
)
#> [1] "feline" "canine" "feline" "canine"
caps_words <- c("caterpillar", "dogwood", "Catastrophe", "DOGMA")
grep_case(
caps_words,
"cat" ~ "feline",
"dog" ~ "canine",
ignore.case = TRUE
)
#> [1] "feline" "canine" "feline" "canine"
countries <- c(
"France", "Ostdeutschland", "Westdeutschland", "Nederland",
"Belgie (Vlaanderen)", "Belgique (Wallonie)", "Luxembourg", "Italia"
)
grep_case(
countries,
"Deutschland" ~ "Germany",
"Belgi(qu)?e" ~ "Belgium",
"Nederland" ~ "Netherlands",
"Italia" ~ "Italy",
.preserve = TRUE,
ignore.case = TRUE
)
#> [1] "France" "Germany" "Germany" "Netherlands" "Belgium"
#> [6] "Belgium" "Luxembourg" "Italy"