recode_chr

recode_chr(df, col, old_names, new_name, regex = FALSE, negate = FALSE)

Arguments

df

data frame

col

unquoted col

old_names

character vector or regular expression

new_name

atomic chr string

regex

Logical, default F. Specify elements for old_names using a regex?

negate

logical, defailt F. If negating the regex, set to T

Value

df

Examples


# Use a negative regex to rename all species other than "virginica" to "none"

iris %>%
   recode_chr(
 col = Species,
 old_names = "vir",
 new_name = "none",
 regex = TRUE,
 negate = TRUE) %>%
 dplyr::count(Species)
#>     Species   n
#> 1      none 100
#> 2 virginica  50

# Specify old names using a regex

iris %>%
   recode_chr(
   col = Species,
   old_names = "set|vir",
   new_name = "other",
   regex = TRUE) %>%
   dplyr::count(Species)
#>      Species   n
#> 1      other 100
#> 2 versicolor  50