Last active
February 23, 2017 01:29
-
-
Save flodel/4470993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decode <- function(x, search, replace, default = NULL) { | |
# build a nested ifelse function by recursion | |
decode.fun <- function(search, replace, default = NULL) | |
if (length(search) == 0L) { | |
function(x) if (is.null(default)) x else rep(default, length(x)) | |
} else { | |
function(x) ifelse(x == search[1L], replace[1L], | |
decode.fun(tail(search, -1L), | |
tail(replace, -1L), | |
default)(x)) | |
} | |
return(decode.fun(search, replace, default)(x)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment