-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Open
Description
Some fodder removed from the function docs that could be better served in the vignette
The 2x2 table
| Create a new column | Update an existing column | |
|---|---|---|
| Logical conditions | case_when | replace_when |
| Values | recode_values | replace_values |
Function connections
#' @section Connection between `case_when()` and `replace_when()`:
#'
#' The following two statements produce identical `result`s:
#'
#' ```r
#' result <- replace_when(x, lhs ~ rhs)
#'
#' result <- case_when(lhs ~ rhs, .default = x, .ptype = x, .size = vec_size(x))
#' result <- vec_set_names(result, vec_names(x))
#' ```
#'
#' If you are replacing a few values within an existing vector, then
#' `replace_when()` is always a better choice because it is type stable and size
#' stable on `x`, pipes better, and better expresses intent.#' @section Connection between `recode_values()` and `case_when()`:
#'
#' While [case_when()] is a general vectorized [if_else()] that uses logical
#' expressions, `recode_values()` is a general vectorized [switch()] that uses
#' values. The following two statements are roughly equivalent:
#'
#' ```
#' case_when(
#' x %in% c("a", "b") ~ 1,
#' x %in% "c" ~ 2,
#' x %in% c("d", "e") ~ 3
#' )
#'
#' recode_values(
#' x,
#' c("a", "b") ~ 1,
#' "c" ~ 2,
#' c("d", "e") ~ 3
#' )
#' ```
#'
#' For large inputs `recode_values()` is going to be much faster than
#' the equivalent `case_when()` statement.#' @section Connection between `recode_values()` and `replace_values()`:
#'
#' The following two statements produce identical `result`s:
#'
#' ```r
#' result <- replace_values(x, lhs ~ rhs)
#'
#' result <- recode_values(x, lhs ~ rhs, default = x, ptype = x)
#' result <- vec_set_names(result, vec_names(x))
#' ```Replaceable with replace_values() or replace_when() (from #7742)
- The idea of
na_if()with >1 RHS values - The idea of
na_if()with a logical condition coalesce(x, 0)orcoalesce(x, y)but notcoalesce(x, y, z)tidyr::replace_na()
SQL connections
#' `case_when()` is an R equivalent of the SQL "searched" `CASE WHEN` statement.#' In the SQL world, `case_when()` is an R equivalent of the SQL "searched"
#' `CASE WHEN` statement, and `recode_values()` is an R equivalent of the SQL
#' "simple" `CASE WHEN` statement.Metadata
Metadata
Assignees
Labels
No labels