in_case_fct {incase} | R Documentation |
Case statements returning a factor
Description
These functions are equivalent to in_case()
, switch_case()
,
grep_case()
, fn_case()
, and fn_switch_case()
but return
factors with their levels determined by the order of their
case statements.
Usage
in_case_fct(..., preserve = FALSE, default = NA, ordered = FALSE)
switch_case_fct(x, ..., preserve = FALSE, default = NA, ordered = FALSE)
grep_case_fct(x, ..., preserve = FALSE, default = NA, ordered = FALSE)
fn_case_fct(x, fn, ..., preserve = FALSE, default = NA, ordered = FALSE)
fn_switch_case_fct(x, fn, ..., preserve = FALSE, default = NA, ordered = FALSE)
Arguments
... |
<
|
preserve |
If |
default |
If |
ordered |
A logical.
If |
x |
A vector |
fn |
A function to apply to the left-hand side of each formula in Either a quoted or unquoted function name, an anonymous The function should take two inputs, the first being |
Value
A factor vector of length 1 or n, matching the length of the logical
input or output vectors.
Levels are determined by the order of inputs to ...
.
Inconsistent lengths will generate an error.
See Also
in_case()
, switch_case()
, grep_case()
, fn_case()
, and
fn_case_fct()
on which these functions are based.
Examples
1:10 %>%
in_case_fct(
. %% 2 == 0 ~ "even",
. %% 2 == 1 ~ "odd"
)
switch_case_fct(
c("a", "b", "c"),
"c" ~ "cantaloupe",
"b" ~ "banana",
"a" ~ "apple"
)
switch_case_fct(
c("a", "b", "c", "d"),
"c" ~ "cantaloupe",
"b" ~ "banana",
"a" ~ "apple"
)
switch_case_fct(
c("a", "b", "c", "d"),
"c" ~ "cantaloupe",
"b" ~ "banana",
"a" ~ "apple",
preserve = TRUE
)
grep_case_fct(
c("caterpillar", "dogwood", "catastrophe", "dogma"),
"cat" ~ "feline",
"dog" ~ "canine"
)
fn_case_fct(
c("a", "b", "c"),
`%in%`,
"c" ~ "cantaloupe",
"b" ~ "banana",
"a" ~ "apple"
)