Skip to content

Commit

Permalink
Rewrite case_when() using vec_case_when() (#6300)
Browse files Browse the repository at this point in the history
* Update `case_when()` to use `vec_case_when()`

* Update internal usage of `case_when()` to use `.default`

* NEWS bullet

* Update NEWS.md

Co-authored-by: Lionel Henry <lionel.hry@gmail.com>

* Clarify usefulness of LHS/RHS recycling

* Make two sided formula check a special case for better error reporting

* Actually generate the new snapshots in `test-vec-case-when.R`

* Use `as_label()` instead of custom deparser

* Restructure `list_names()` as `names_as_error_names()`

* Use `n_processed` over ambiguous `n_used`

* Use better variable name for unused locations

* Allow `vec_case_when()` to own its errors since it might be user facing

* Mention how to handle `NA` behavior yourself

Hints at the fact that this has to be tailored to your specific usage of `case_when()`, making it hard to generalize (like with a `.missing` argument)

Co-authored-by: Lionel Henry <lionel.hry@gmail.com>
  • Loading branch information
DavisVaughan and lionel- authored Jul 1, 2022
1 parent 8b86d6d commit 9027289
Show file tree
Hide file tree
Showing 12 changed files with 1,335 additions and 214 deletions.
33 changes: 33 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
# dplyr (development version)

* `case_when()` has been rewritten to utilize vctrs (#5106). This comes with a
number of useful improvements:

* There is a new `.default` argument that is intended to replace usage of
`TRUE ~ default_value` as a more explicit and readable way to specify
a default value. In the future, we will deprecate the unsafe recycling of
the LHS inputs that allows `TRUE ~` to work, so we encourage you to switch
over to using `.default` instead.

* The types of the RHS inputs no longer have to match exactly. For example,
the following no longer requires you to use `NA_character_` instead of just
`NA`.

```
x <- c("little", "unknown", "small", "missing", "large")
case_when(
x %in% c("little", "small") ~ "one",
x %in% c("big", "large") ~ "two",
x %in% c("missing", "unknown") ~ NA
)
```
* `case_when()` now supports a larger variety of value types. For example,
you can use a data frame to create multiple columns at once.
* There are new `.ptype` and `.size` arguments which allow you to enforce
a particular output type and size. This allows you to construct a completely
type and size stable call to `case_when()`.
* The error thrown when types or lengths were incorrect has been improved
(#6261, #6206).
* `arrange()` now uses a faster algorithm for sorting character vectors, which
is heavily inspired by data.table's `forder()`. Additionally, the default
locale for sorting character vectors is now the C locale, which is a breaking
Expand Down
Loading

0 comments on commit 9027289

Please sign in to comment.