Each case is evaluated for all cases and a character vector match
for each element determines the corresponding value in the output vector.
If no cases match, the .default is used.
The function allows you to assign multiple values to a character value, which
can be very handy for EDA.
Arguments
- ...
<
dynamic-dots> A sequence of two-sided formulas. The left hand side (LHS) determines which values match this case. The right hand side (RHS) provides the replacement value.For
case_when():The LHS inputs must be logical vectors. For backwards compatibility, scalars are recycled, but we no longer recommend supplying scalars.
The RHS inputs will be cast to their common type, and will be recycled to the common size of the LHS inputs.
For
replace_when():The LHS inputs must be logical vectors the same size as
x.The RHS inputs will be cast to the type of
xand recycled to the size ofx.
NULLinputs are ignored.- .default
The value used when all of the LHS inputs return either
FALSEorNA.If
NULL, the default, a missing value will be used.If provided,
.defaultwill follow the same type and size rules as the RHS inputs.
NAvalues in the LHS conditions are treated likeFALSE, meaning that the result at those locations will be assigned the.defaultvalue. To handle missing values in the conditions differently, you must explicitly catch them with another condition before they fall through to the.default. This typically involves some variation ofis.na(x) ~ valuetailored to your usage ofcase_when().- .sep,
the separator between answers. (default is
;), can't be a substring of any of the text- .drop_empty
drop if no match is returned. (Defaults to
TRUEfor legibility), but ifFALSE, can be used more easily withtidyr::separate_wider/longer_delim()
Value
For case_when(), a new vector where the size is the common size of the LHS
inputs, the type is the common type of the RHS inputs, and the names
correspond to the names of the RHS elements used in the result.
For replace_when(), an updated version of x, with the same size, type,
and names as x.
Examples
case_if_any(
mtcars$vs == 1 ~ "vs = 1",
mtcars$mpg > 150 ~ "I have mpg > 150"
)
#> [1] "" "" "vs = 1" "vs = 1" "" "vs = 1" "" "vs = 1"
#> [9] "vs = 1" "vs = 1" "vs = 1" "" "" "" "" ""
#> [17] "" "vs = 1" "vs = 1" "vs = 1" "vs = 1" "" "" ""
#> [25] "" "vs = 1" "" "vs = 1" "" "" "" "vs = 1"
case_if_any(
mtcars$vs == 1 ~ "Woww",
mtcars$mpg > 15 ~ "QW",
mtcars$qsec > 18 ~ "ooh lalal",
.sep = ";",
.default = NA
)
#> [1] "QW" "QW" "Woww;QW;ooh lalal"
#> [4] "Woww;QW;ooh lalal" "QW" "Woww;QW;ooh lalal"
#> [7] NA "Woww;QW;ooh lalal" "Woww;QW;ooh lalal"
#> [10] "Woww;QW;ooh lalal" "Woww;QW;ooh lalal" "QW"
#> [13] "QW" "QW" NA
#> [16] NA NA "Woww;QW;ooh lalal"
#> [19] "Woww;QW;ooh lalal" "Woww;QW;ooh lalal" "Woww;QW;ooh lalal"
#> [22] "QW" "QW" NA
#> [25] "QW" "Woww;QW;ooh lalal" "QW"
#> [28] "Woww;QW" "QW" "QW"
#> [31] NA "Woww;QW;ooh lalal"