From 707b332c3c499f966bb0515a3ab90711b7104147 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Mon, 23 Jan 2023 08:14:07 -0600 Subject: [PATCH] Fix case_when() logic error Fixes #1227 --- logicals.qmd | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/logicals.qmd b/logicals.qmd index 6ca5cf6..7ec3c76 100644 --- a/logicals.qmd +++ b/logicals.qmd @@ -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.