parent
6b92efbf52
commit
707b332c3c
|
@ -488,6 +488,7 @@ It takes pairs that look like `condition ~ output`.
|
||||||
This means we could recreate our previous nested `if_else()` as follows:
|
This means we could recreate our previous nested `if_else()` as follows:
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
|
x <- c(-3:3, NA)
|
||||||
case_when(
|
case_when(
|
||||||
x == 0 ~ "0",
|
x == 0 ~ "0",
|
||||||
x < 0 ~ "-ve",
|
x < 0 ~ "-ve",
|
||||||
|
@ -538,13 +539,15 @@ flights |>
|
||||||
arr_delay < -30 ~ "very early",
|
arr_delay < -30 ~ "very early",
|
||||||
arr_delay < -15 ~ "early",
|
arr_delay < -15 ~ "early",
|
||||||
abs(arr_delay) <= 15 ~ "on time",
|
abs(arr_delay) <= 15 ~ "on time",
|
||||||
arr_delay > 15 ~ "late",
|
arr_delay < 60 ~ "late",
|
||||||
arr_delay > 60 ~ "very late",
|
arr_delay < Inf ~ "very late",
|
||||||
),
|
),
|
||||||
.keep = "used"
|
.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
|
### Compatible types
|
||||||
|
|
||||||
Note that both `if_else()` and `case_when()` require **compatible** types in the output.
|
Note that both `if_else()` and `case_when()` require **compatible** types in the output.
|
||||||
|
|
Loading…
Reference in New Issue