Fix case_when() logic error

Fixes #1227
This commit is contained in:
Hadley Wickham 2023-01-23 08:14:07 -06:00
parent 6b92efbf52
commit 707b332c3c
1 changed files with 5 additions and 2 deletions

View File

@ -488,6 +488,7 @@ It takes pairs that look like `condition ~ output`.
This means we could recreate our previous nested `if_else()` as follows:
```{r}
x <- c(-3:3, NA)
case_when(
x == 0 ~ "0",
x < 0 ~ "-ve",
@ -538,13 +539,15 @@ flights |>
arr_delay < -30 ~ "very early",
arr_delay < -15 ~ "early",
abs(arr_delay) <= 15 ~ "on time",
arr_delay > 15 ~ "late",
arr_delay > 60 ~ "very late",
arr_delay < 60 ~ "late",
arr_delay < Inf ~ "very late",
),
.keep = "used"
)
```
Be wary when writing this sort of complex `case_when()` statement; my first two attempts used a mix of `<` and `>` and I kept accidentally creating overlapping conditions.
### Compatible types
Note that both `if_else()` and `case_when()` require **compatible** types in the output.