forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement recode(), a vectorised switch().
Includes extraction of replace_with() function that is also used in if_else() and coalesce(). Fixes tidyverse#1710.
- Loading branch information
Showing
13 changed files
with
287 additions
and
59 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#' Recode values | ||
#' | ||
#' This is a vectorised version of \code{\link{switch}()}: you can replace | ||
#' numeric values based on their position, and character values by their | ||
#' name. This is an S3 generic: dplyr provides methods for numeric, character, | ||
#' and factors. For logical vectors, use \code{\link{if_else}} | ||
#' | ||
#' @param x A vector to modify | ||
#' @param ... Replacments. These should be named for character and factor | ||
#' \code{x}. All replacements must be the same type, and must have either | ||
#' length one or the same length as x. | ||
#' @param default If supplied, all values not otherwise matched will be | ||
#' given this value instead of \code{NA}. Must be either length 1 or the same | ||
#' length as \code{x}. | ||
#' @param missing If supplied, any missing values in \code{x} will be | ||
#' replaced by this value. Must be either length 1 or the same length as | ||
#' \code{x}. | ||
#' @return A vector the same length as \code{x}, and the same type as the | ||
#' first of \code{...}, \code{default}, or \code{missing}. | ||
#' @export | ||
#' @examples | ||
#' x <- c(1:5, NA) | ||
#' recode(x, "a", "b", "c") | ||
#' recode(x, "a", "b", "c", default = "other") | ||
#' recode(x, "a", "b", "c", default = "other", missing = "missing") | ||
#' | ||
#' # Use named arguments with a character vector | ||
#' x <- sample(c("a", "b", "c"), 10, replace = TRUE) | ||
#' recode(x, a = "Apple") | ||
#' recode(x, a = "Apple", default = x) | ||
recode <- function(x, ..., default = NULL, missing = NULL) { | ||
UseMethod("recode") | ||
} | ||
|
||
#' @export | ||
recode.numeric <- function(x, ..., default = NULL, missing = NULL) { | ||
values <- list(...) | ||
if (any(has_names(values))) { | ||
warning("Names are ignored", call. = FALSE) | ||
} | ||
|
||
n <- length(x) | ||
template <- find_template(..., default, missing) | ||
out <- template[rep(NA_integer_, n)] | ||
replaced <- rep(FALSE, n) | ||
|
||
for (i in seq_along(values)) { | ||
out <- replace_with(out, x == i, values[[i]], paste0("Vector ", i)) | ||
replaced[x == i] <- TRUE | ||
} | ||
|
||
out <- replace_with(out, !replaced & !is.na(x), default, "`default`") | ||
out <- replace_with(out, is.na(x), missing, "`missing`") | ||
out | ||
} | ||
|
||
#' @export | ||
recode.character <- function(x, ..., default = NULL, missing = NULL) { | ||
values <- list(...) | ||
if (!all(has_names(values))) { | ||
stop("All replacements must be named", call. = FALSE) | ||
} | ||
|
||
n <- length(x) | ||
template <- find_template(..., default, missing) | ||
out <- template[rep(NA_integer_, n)] | ||
replaced <- rep(FALSE, n) | ||
|
||
for (nm in names(values)) { | ||
out <- replace_with(out, x == nm, values[[nm]], paste0("`", nm, "`")) | ||
replaced[x == nm] <- TRUE | ||
} | ||
|
||
out <- replace_with(out, !replaced & !is.na(x), default, "`default`") | ||
out <- replace_with(out, is.na(x), missing, "`missing`") | ||
out | ||
} | ||
|
||
#' @export | ||
recode.factor <- function(x, ..., default = NULL, missing = NULL) { | ||
values <- list(...) | ||
if (length(values) == 0) { | ||
stop("No replacements provided", call. = FALSE) | ||
} | ||
|
||
if (!all(has_names(values))) { | ||
stop("All replacements must be named", call. = FALSE) | ||
} | ||
if (!is.null(missing)) { | ||
stop("`missing` is not supported for factors", call. = FALSE) | ||
} | ||
|
||
out <- rep(NA_character_, length(levels(x))) | ||
replaced <- rep(FALSE, length(levels(x))) | ||
|
||
for (nm in names(values)) { | ||
out <- replace_with(out, levels(x) == nm, values[[nm]], paste0("`", nm, "`")) | ||
replaced[levels(x) == nm] <- TRUE | ||
} | ||
|
||
out <- replace_with(out, !replaced, default, "`default`") | ||
levels(x) <- out | ||
|
||
x | ||
} | ||
|
||
find_template <- function(...) { | ||
x <- compact(list(...)) | ||
|
||
if (length(x) == 0) { | ||
stop("No replacements provided", call. = FALSE) | ||
} | ||
|
||
x[[1]] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
replace_with <- function(x, i, val, name) { | ||
if (is.null(val)) { | ||
return(x) | ||
} | ||
|
||
check_length(val, x, name) | ||
check_type(val, x, name) | ||
check_class(val, x, name) | ||
|
||
if (length(val) == 1L) { | ||
x[i] <- val | ||
} else { | ||
x[i] <- val[i] | ||
} | ||
|
||
x | ||
} | ||
|
||
check_length <- function(x, template, name = deparse(substitute(x))) { | ||
n <- length(template) | ||
if (length(x) == n) { | ||
return() | ||
} | ||
|
||
if (length(x) == 1L) { | ||
return() | ||
} | ||
|
||
stop(name, " is length ", length(x), " not 1 or ", n, ".", call. = FALSE) | ||
} | ||
|
||
check_type <- function(x, template, name = deparse(substitute(x))) { | ||
if (identical(typeof(x), typeof(template))) { | ||
return() | ||
} | ||
|
||
stop( | ||
name, " has type '", typeof(x), "' not '", typeof(template), "'", | ||
call. = FALSE | ||
) | ||
} | ||
|
||
check_class <- function(x, template, name = deparse(substitute(x))) { | ||
if (!is.object(x)) { | ||
return() | ||
} | ||
|
||
if (identical(class(x), class(template))) { | ||
return() | ||
} | ||
|
||
stop(name, " has class ", paste(class(x), collapse = "/"), " not ", | ||
paste(class(template), collapse = "/"), call. = FALSE) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.