forcats icon indicating copy to clipboard operation
forcats copied to clipboard

Feature Request: Add an "after" argument to fct_expand

Open billdenney opened this issue 7 years ago • 1 comments

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)
}

billdenney avatar Jul 17 '18 14:07 billdenney

Before this can be implemented we need to review the interface to make sure it's consistent with other tidyverse functions.

hadley avatar Jan 04 '19 17:01 hadley

Seems reasonable to use after to match fct_relevel().

hadley avatar Jan 03 '23 20:01 hadley

Thanks!

billdenney avatar Jan 04 '23 20:01 billdenney