-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function that creates factor in order of case_when matches #298
Comments
I think we'd need to make the syntax more limiting than Since we'd want to restrict each expression to a single character level, we could put it in the LHS of
But I don't know if any existing tidyverse function uses similar syntax. |
I do think removing the usage of |
FWIW this is basically how |
Will wait until lower level functions are exposed by vctrs. |
I would think it would be convenient to solve this from the Something like this: set.seed(2022)
x <- sample(
c("low", "intermediate", "high"),
prob = c(0.5, 0.2, 0.3),
size = 100,
replace = TRUE
)
z <- rbinom(
n = 100,
size = 100,
prob = 0.3
)
y <- case_when(
x == "intermediate" | (x == "low" & z < 30) ~ "B",
x == "low" ~ "A",
x == "high" ~ "C",
TRUE ~ NA_character_,
.ptype = "factor"
) |
A common workflow I do is map one vector to another using some (possibly complex) conditions, then coerce to a factor with the level order the same as parsed in
dplyr::case_when()
. It would be helpful if there was a wrapper that created the factor without having to manually specify the levels.Currently, I'd do something like this:
Created on 2022-02-01 by the reprex package (v2.0.1)
Can we add a function that makes
y
into a factor with the level order the same as specified in thecase_when()
? For example,The text was updated successfully, but these errors were encountered: