fct_yesno | R Documentation |
Format factor levels as arbitrary values of Yes/No (with Yes always first) while leaving untouched all vectors that contain other information.
fct_yesno(
x,
input = list(yes = c("Yes", "Oui"), no = c("No", "Non")),
output = c("Yes", "No"),
strict = FALSE,
mutate_character = TRUE,
fail = TRUE
)
x |
a vector of any type/class. |
input |
list of values to be considered as "yes" and "no". |
output |
the output factor levels. |
strict |
whether to match the input strictly or use stringr::str_detect to find them. |
mutate_character |
whether to turn characters into factor. |
fail |
whether to fail if some levels cannot be recoded to yes/no. |
a factor, or x
untouched.
fct_yesno(c("No", "Yes")) #levels are in order
set.seed(42)
N=6
x = tibble(
a=sample(c("Yes", "No"), size=N, replace=TRUE),
b=sample(c("Oui", "Non"), size=N, replace=TRUE),
c=sample(0:1, size=N, replace=TRUE),
d=sample(c(TRUE, FALSE), size=N, replace=TRUE),
e=sample(c("1-Yes", "0-No"), size=N, replace=TRUE),
y=sample(c("aaa", "bbb", "ccc"), size=N, replace=TRUE),
z=1:N,
)
x
#y and z are left untouched (or throw an error if fail=TRUE)
sapply(x, fct_yesno, fail=FALSE)
# as "1-Yes" is not in `input`, x$e is untouched/fails if strict=TRUE
fct_yesno(x$e)
fct_yesno(x$e, strict=TRUE, fail=FALSE)
fct_yesno(x$e, output=c("Ja", "Nein"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.