-
Notifications
You must be signed in to change notification settings - Fork 131
Open
Labels
featurea feature request or enhancementa feature request or enhancementwipwork in progresswork in progress
Description
While fct_lump()
currently allows us to keep the N
most frequent factors (lumping the rest into "Other"), I would love to have an option to this function (call it "inorder") which would instead preserve the first N
factors, lumping the rest into "Other".
library(magrittr)
library(forcats)
f <- c("hello","goodbye","ciao","arrivederci","ciao","hello") %>% fct_inorder
f
#> [1] hello goodbye ciao arrivederci
#> [5] ciao hello
#> Levels: hello goodbye ciao arrivederci
To preserve the first two appearing levels of the factor, create function fct_lump_inorder()
, all other levels are lumped into "Other":
f %>% fct_lump_inorder(2)
#> [1] hello goodbye Other Other
#> [5] Other hello
Levels: hello goodbye Other
Notice this is very useful if I have reordered a factor using fct_reorder()
and now only want the first N of the list (akin to arrange()
+ head()
). Example:
df %>%
mutate(col1 = col1 %>% fct_reorder(-col2) %>% fct_lump_inorder(4)) %>%
ggplot(aes(...)) + ....
TlaskalV, ginolhac and bayer-lsettles
Metadata
Metadata
Assignees
Labels
featurea feature request or enhancementa feature request or enhancementwipwork in progresswork in progress