Skip to content

Instantly share code, notes, and snippets.

@flodel
Last active February 23, 2017 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save flodel/4470993 to your computer and use it in GitHub Desktop.
Save flodel/4470993 to your computer and use it in GitHub Desktop.
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