I\'m a fan of the revalue
function is plyr
for substituting strings. It\'s simple and easy to remember.
However, I\'ve migrated new code to
I wanted to comment on the answer by @aosmith, but lack reputation. It seems that nowadays the default of dplyr
's recode
function is to leave unspecified levels unaffected.
x = sample(c("a", "b", "c"), 10, replace = TRUE)
x
[1] "c" "c" "b" "b" "a" "b" "c" "c" "c" "b"
recode(x , a = "apple", b = "banana" )
[1] "c" "c" "banana" "banana" "apple" "banana" "c" "c" "c" "banana"
To change all nonspecified levels to NA
, the argument .default = NA_character_
should be included.
recode(x, a = "apple", b = "banana", .default = NA_character_)
[1] "apple" "banana" "apple" "banana" "banana" "apple" NA NA NA "apple"