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
By building a nested ifelse
functional by recursion, you can get the benefits of both solutions offered so far: ifelse
is fast and can work with any type of data, while @Matthew's solution is more functional yet limited to integers and potentially slow.
decode <- function(x, search, replace, default = NULL) {
# build a nested ifelse function by recursion
decode.fun <- function(search, replace, default = NULL)
if (length(search) == 0) {
function(x) if (is.null(default)) x else rep(default, length(x))
} else {
function(x) ifelse(x == search[1], replace[1],
decode.fun(tail(search, -1),
tail(replace, -1),
default)(x))
}
return(decode.fun(search, replace, default)(x))
}
Note how the decode
function is named after the SQL function. I wish a function like this made it to the base R package... Here are a couple examples illustrating its usage:
decode(x = 1:5, search = 3, replace = -1)
# [1] 1 2 -1 4 5
decode(x = 1:5, search = c(2, 4), replace = c(20, 40), default = 3)
# [1] 3 20 3 40 3
For your particular problem:
transform(z, q = decode(x, search = c(2,4,7), replace = c(2,4,3), default = 1) * t)
# x y t q
# 1 1 11 21 21
# 2 2 12 22 44
# 3 3 13 23 23
# 4 4 14 24 96
# 5 5 15 25 25
# 6 6 16 26 26
# 7 7 17 27 81
# 8 8 18 28 28
# 9 9 19 29 29
# 10 10 20 30 30