I have a dataframe z and I want to create the new column based on the values of two old columns of z. Following is the process:
&g
Here's a version of an SQL decode in R for character vectors (untested with factors) that operates just like the SQL version. i.e. it takes an arbitrary number of target/replacement pairs, and optional last argument that acts as a default value (note that the default won't overwrite NAs).
I can see it being pretty useful in conjunction with dplyr's mutate operation.
> x <- c("apple","apple","orange","pear","pear",NA)
> decode(x, apple, banana)
[1] "banana" "banana" "orange" "pear" "pear" NA
> decode(x, apple, banana, fruit)
[1] "banana" "banana" "fruit" "fruit" "fruit" NA
> decode(x, apple, banana, pear, passionfruit)
[1] "banana" "banana" "orange" "passionfruit" "passionfruit" NA
> decode(x, apple, banana, pear, passionfruit, fruit)
[1] "banana" "banana" "fruit" "passionfruit" "passionfruit" NA
Here's the code I'm using, with a gist I'll keep up to date here (link).
decode <- function(x, ...) {
args <- as.character((eval(substitute(alist(...))))
replacements <- args[1:length(args) %% 2 == 0]
targets <- args[1:length(args) %% 2 == 1][1:length(replacements)]
if(length(args) %% 2 == 1)
x[! x %in% targets & ! is.na(x)] <- tail(args,1)
for(i in 1:length(targets))
x <- ifelse(x == targets[i], replacements[i], x)
return(x)
}