This is a vectorised version of switch(): you can replace numeric values based on their position, and character values by their name. This is an S3 generic: dplyr provides methods for numeric, character, and factors. For logical vectors, use if_else(). For more complicated criteria, use case_when().

recode(.x, ..., .default = NULL, .missing = NULL)

recode_factor(.x, ..., .default = NULL, .missing = NULL, .ordered = FALSE)

Arguments

.x

A vector to modify

...

Replacements. These should be named for character and factor .x, and can be named for numeric .x. The argument names should be the current values to be replaced, and the argument values should be the new (replacement) values.

All replacements must be the same type, and must have either length one or the same length as x.

These dots are evaluated with explicit splicing.

.default

If supplied, all values not otherwise matched will be given this value. If not supplied and if the replacements are the same type as the original values in .x, unmatched values are not changed. If not supplied and if the replacements are not compatible, unmatched values are replaced with NA. .default must be either length 1 or the same length as .x.

.missing

If supplied, any missing values in .x will be replaced by this value. Must be either length 1 or the same length as .x.

.ordered

If TRUE, recode_factor() creates an ordered factor.

Value

A vector the same length as .x, and the same type as the first of ..., .default, or .missing. recode_factor() returns a factor whose levels are in the same order as in ....

Details

You can use recode() directly with factors; it will preserve the existing order of levels while changing the values. Alternatively, you can use recode_factor(), which will change the order of levels to match the order of replacements. See the forcats package for more tools for working with factors and their levels.

Examples

# Recode values with named arguments x <- sample(c("a", "b", "c"), 10, replace = TRUE) recode(x, a = "Apple")
#> [1] "b" "b" "Apple" "c" "c" "Apple" "c" "c" "Apple" #> [10] "c"
recode(x, a = "Apple", .default = NA_character_)
#> [1] NA NA "Apple" NA NA "Apple" NA NA "Apple" #> [10] NA
# Named arguments also work with numeric values x <- c(1:5, NA) recode(x, `2` = 20L, `4` = 40L)
#> [1] 1 20 3 40 5 NA
# Note that if the replacements are not compatible with .x, # unmatched values are replaced by NA and a warning is issued. recode(x, `2` = "b", `4` = "d")
#> Warning: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default
#> [1] NA "b" NA "d" NA NA
# If you don't name the arguments, recode() matches by position recode(x, "a", "b", "c")
#> Warning: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default
#> [1] "a" "b" "c" NA NA NA
recode(x, "a", "b", "c", .default = "other")
#> [1] "a" "b" "c" "other" "other" NA
recode(x, "a", "b", "c", .default = "other", .missing = "missing")
#> [1] "a" "b" "c" "other" "other" "missing"
# Supply default with levels() for factors x <- factor(c("a", "b", "c")) recode(x, a = "Apple", .default = levels(x))
#> [1] Apple b c #> Levels: Apple b c
# Use recode_factor() to create factors with levels ordered as they # appear in the recode call. The levels in .default and .missing # come last. x <- c(1:4, NA) recode_factor(x, `1` = "z", `2` = "y", `3` = "x")
#> Warning: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default
#> [1] z y x <NA> <NA> #> Levels: z y x
recode_factor(x, `1` = "z", `2` = "y", .default = "D")
#> [1] z y D D <NA> #> Levels: z y D
recode_factor(x, `1` = "z", `2` = "y", .default = "D", .missing = "M")
#> [1] z y D D M #> Levels: z y D M
# When the input vector is a compatible vector (character vector or # factor), it is reused as default. recode_factor(letters[1:3], b = "z", c = "y")
#> [1] a z y #> Levels: z y a
recode_factor(factor(letters[1:3]), b = "z", c = "y")
#> [1] a z y #> Levels: z y a