Skip to content

request: allow fct_lump to lump factors based on order of appearance #187

@dan-reznik

Description

@dan-reznik

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(...)) + ....

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions