

# The factor method of `recode()` can generally be replaced with # `forcats::fct_recode()` factor_vec Apple b c #> Levels: Apple b c # `recode_factor()` does not currently have a direct replacement, but we # plan to add one to forcats. default = num_vec) : #> Can't combine `.1 (right)` and `.default`. default = num_vec ) ) #> Error in case_match(num_vec, 2 ~ "b", 4 ~ "d". #> Please specify replacements exhaustively or supply `.default`. default = "nothing" ) #> "a" "nothing" "c" # For `case_match()`, incompatible types are an error rather than a warning recode ( num_vec, `2` = "b", `4` = "d" ) #> Warning: Unreplaced values treated as NA as `.x` is not compatible. default = num_vec ) #> 1 20 3 40 NA # `case_match()` doesn't have the ability to match by position like # `recode()` does with numeric vectors recode ( num_vec, "a", "b", "c", "d" ) #> "a" "b" "c" "d" NA recode ( c ( 1, 5, 3 ), "a", "b", "c", "d". # `case_match()` is easier to use with numeric vectors, because you don't # need to turn the numeric values into names num_vec 1 20 3 40 NA case_match ( num_vec, 2 ~ 20, 4 ~ 40. default = NA) : #> `.default` must be a character vector, not `NA`. default = NA ) ) #> Error in recode(char_vec, a = "Apple", b = "Banana". try ( recode ( char_vec, a = "Apple", b = "Banana".

default = NA ) #> "Apple" "Apple" "Apple" "Apple" NA NA "Banana" #> "Apple" "Banana" NA # Throws an error as `NA` is logical, not character. default = NA_character_ ) #> "Apple" "Apple" "Apple" "Apple" NA NA "Banana" #> "Apple" "Banana" NA case_match ( char_vec, "a" ~ "Apple", "b" ~ "Banana". default = char_vec ) #> "Apple" "Apple" "Apple" "Apple" "c" "c" "Banana" #> "Apple" "Banana" "c" # With `case_match()`, you don't need typed missings like `NA_character_` recode ( char_vec, a = "Apple", b = "Banana". Must be either length 1 or the same length asĬhar_vec "Apple" "Apple" "Apple" "Apple" "c" "c" "Banana" #> "Apple" "Banana" "c" case_match ( char_vec, "a" ~ "Apple", "b" ~ "Banana". default must be either length 1 or the same length as If not supplied and if the replacementsĪre not compatible, unmatched values are replaced with NA. If not supplied and if the replacements are If supplied, all values not otherwise matched willīe given this value. When named, the argument names should be the current values to be replaced, and theĪrgument values should be the new (replacement) values.Īll replacements must be the same type, and must have either

x represents positions to look for in replacements. If not named, the replacement is done based on position i.e. x, these should be namedĪnd replacement is based only on their name.
