DavisVaughan · GitHub

Merged

Merged

Conversation

DavisVaughan

Comment on lines +3 to +12

* `case_when()` is now part of a family of 4 related functions, 3 of which are new:

* Use `case_when()` to create a new vector based on logical conditions.
* Use `replace_when()` to update an existing vector based on logical conditions.
* Use `recode_values()` to create a new vector by mapping old values to new values.
* Use `replace_values()` to update an existing vector by mapping old values to new values.

We are particularly excited about `recode_values()`, which allows you to easily incorporate a lookup table, and serves as a more holistic replacement for both `case_match()` and `recode()`.

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.
@@ -1,36 +1,83 @@
#' A general vectorised if-else
#'
#' @description

Comment on lines +405 to +412

test_that("replace_when() does not recycle LHS values", {
# Unlike `case_when()` we get to do this right!
x <- c(1, 2, 3)

expect_snapshot(error = TRUE, {
replace_when(x, TRUE ~ 0)
})
})

Comment on lines +462 to +467

test_that("replace_when() does not allow named `...`", {
# Purposefully stricter than `case_when()`
expect_snapshot(error = TRUE, {
replace_when(1, foo = TRUE ~ 2)
})
})

Comment on lines +476 to +482

test_that("replace_when() is a no-op with zero conditions", {
# Unlike `case_when()`, where when zero conditions are supplied
# we don't know what kind of vector to build (and we refuse to
# build an `unspecified` vector, unlike `vec_case_when()`)
expect_identical(replace_when(1), 1)
expect_identical(replace_when(1, NULL), 1)
})

@DavisVaughan

@DavisVaughan

Merged

#' For `case_when()`:
#'
#' The RHS inputs will be coerced to their common type.
#' - The LHS inputs must be logical vectors.

@DavisVaughan

@DavisVaughan

Labels

None yet

Read the original on github.com ↗