Skip to content

Commit eda7ccd

Browse files
committed
Implement replace_when()
1 parent 2f9a846 commit eda7ccd

File tree

9 files changed

+630
-243
lines changed

9 files changed

+630
-243
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export(rename_if)
384384
export(rename_vars)
385385
export(rename_vars_)
386386
export(rename_with)
387+
export(replace_when)
387388
export(right_join)
388389
export(row_number)
389390
export(rows_append)

NEWS.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# dplyr (development version)
22

3+
* `case_when()` is now part of a family of 4 related functions, 3 of which are new:
4+
5+
* Use `case_when()` to create a new vector based on logical conditions.
6+
* Use `replace_when()` to update an existing vector based on logical conditions.
7+
* Use `recode_values()` to create a new vector by mapping old values to new values.
8+
* Use `replace_values()` to update an existing vector by mapping old values to new values.
9+
10+
`replace_when()` is particularly useful for conditionally mutating rows within one or more columns, and can be thought of as an enhanced version of `base::replace()`.
11+
12+
`recode_values()` and `replace_values()` have the familiar `case_when()`-style formula interface for easy interactive use, but also have `from` and `to` arguments as a way for you to incorporate a pre-built lookup table, making them more holistic replacements for both `case_match()` and `recode()`.
13+
14+
This work is a result of [Tidyup 7: Recoding and replacing values in the tidyverse](https://github.com/tidyverse/tidyups/blob/main/007-tidyverse-recoding-and-replacing.md), with a lot of great [feedback](https://github.com/tidyverse/tidyups/pull/29) from the community (#7728).
15+
316
* In `case_when()`, supplying all size 1 LHS inputs along with a size >1 RHS input is now soft-deprecated. This is an improper usage of `case_when()` that should instead be a series of if statements, like:
417

518
```

R/case-match.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,14 @@
123123
#' .keep = "used"
124124
#' )
125125
case_match <- function(.x, ..., .default = NULL, .ptype = NULL) {
126-
args <- list2(...)
126+
# Historically it did not allow this, even though in practice it could allow
127+
# empty dots to be a no-op, like `replace_when()` and `replace_values()`.
128+
allow_empty_dots <- FALSE
127129

128-
args <- case_formula_evaluate(
129-
args = args,
130-
default_env = caller_env(),
131-
dots_env = current_env(),
132-
error_call = current_env()
130+
args <- eval_formulas(
131+
...,
132+
allow_empty_dots = allow_empty_dots
133133
)
134-
135134
haystacks <- args$lhs
136135
values <- args$rhs
137136

0 commit comments

Comments
 (0)