tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
554 stars 126 forks source link

fct_lump_prop() fix for empty levels #310

Closed williamlai2 closed 1 year ago

williamlai2 commented 2 years ago

Fixes the issue of fct_lump_prop() when there are unused levels.

# original example--------------------------------
f <- factor(c("a", "a", "b", "c"), levels = c("a", "b", "c", "d"))
fct_lump_prop(f, prop = 0.25)
# [1] a     a     Other Other
# Levels: a Other

fct_lump_prop(f, prop = 0.25, w = rep(1, 4))
#[1] a     a     Other Other
#Levels: a Other

# Hadley's reprex-----------------------------
f <- factor(c("a", "b", "c"), levels = c("a", "b", "c", "d"))
fct_lump_prop(f, prop = 0.02,  w = c(1,1,1))
#[1] a b c
#Levels: a b c d

# also-------------------------------------
fct_lump_min(f, min = 1,  w = c(1,1,1))
# [1] a b c
# Levels: a b c Other

# previous output
# [1] a b c
# Levels: a b c d

Closes #292

hadley commented 1 year ago

Thanks for working on this, but I think it's a bit better to fix upstream in check_calc_levels().