forcats
forcats copied to clipboard
Feature Request: Add an "after" argument to fct_expand
For fct_expand, it would be useful to be able to insert the new level somewhere other than as the last level in the levels. This is especially true for ordered factors.
Something like the following is what I'm thinking:
fct_expand <- function(f, ..., after=NULL) {
f <- forcats:::check_factor(f)
new_levels <- rlang::chr(...)
if (is.null(after)) {
all_levels <- union(levels(f), new_levels)
} else {
all_levels <-
append(setdiff(levels(f), new_levels),
new_levels,
after=after)
}
lvls_expand(f, new_levels=all_levels)
}
Before this can be implemented we need to review the interface to make sure it's consistent with other tidyverse functions.
Seems reasonable to use after to match fct_relevel().
Thanks!