Compared to dplyr::if_else()
, this function is easier to use with a pipe.
A vector piped into this function will be quietly ignored.
This allows magrittr dots to be used in arguments without requiring
workarounds like wrapping the function in braces.
if_case(condition, true, false, missing = NA, ...)
condition | Logical vector |
---|---|
true, false, missing | Values to use for |
... | Values passed to |
Where condition
is TRUE
, the matching value from true
;
where it's FALSE
, the matching value from false
;
and where it's NA
, the matching value from missing
.
This function is also less strict than dplyr::if_else()
.
If true
, false
, and missing
are different types, they are silently
coerced to a common type.
in_case()
, a pipeable alternative to dplyr::case_when()
switch_case()
, a reimplementation of switch()
dplyr::if_else()
, from which this function is derived
x <- c(1, 2, 5, NA) # if_case() produces the same output as dplyr::if_else() if_case(x > 3, "high", "low", "missing")#> [1] "low" "low" "high" "missing"#> [1] "low" "low" "high" "missing"# if_case() does not throw an error if arguments are not of the same type if_case(x > 3, "high", "low", NA)#> [1] "low" "low" "high" NA#> Error : `missing` must be a character vector, not a logical vector.# if_case() can accept a piped input without an error or requiring braces x %>% if_case(. > 3, "high", "low", "missing")#> [1] "low" "low" "high" "missing"#> Error in dplyr::if_else(., . > 3, "high", "low", "missing") : #> unused argument ("missing")#> [1] "low" "low" "high" "missing"# You can also pipe a conditional test like dplyr::if_else() {x > 3} %>% if_case("high", "low", "missing")#> [1] "low" "low" "high" "missing"#> [1] "low" "low" "high" "missing"