case_when variant that pastes results of matched formulas.
Source:R/case_when_paste.R
case_when_paste.RdDrop-in replacement for case_when(), when you want combined output from the provided formulas.
Evaluates each formula separately, then combines the result per entry.
Drops NA results before pasting - NA should only be supplied in .default if necessary.
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.The LHS inputs must evaluate to logical vectors.
The RHS inputs will be coerced to their common type.
All inputs will be recycled to their common size. That said, we encourage all LHS inputs to be the same size. Recycling is mainly useful for RHS inputs, where you might supply a size 1 input that will be recycled to the size of the LHS inputs.
NULLinputs are ignored.- .default
The value used when all of the LHS inputs return either
FALSEorNA..defaultmust be size 1 or the same size as the common size computed from.....defaultparticipates in the computation of the common type with 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().If
NULL, the default, a missing value will be used.- .sep
The string separator for the different results when being combined.
Examples
x <- data.frame(a = c(1, 2, 3), b = c(1, 2, 4))
x
#> a b
#> 1 1 1
#> 2 2 2
#> 3 3 4
dplyr::mutate(x,
class = case_when_paste(
a == b ~ "equal",
a == 1 ~ "special",
.default = "not equal",
.sep = " - "
)
)
#> a b class
#> 1 1 1 equal - special
#> 2 2 2 equal
#> 3 3 4 not equal