forcats icon indicating copy to clipboard operation
forcats copied to clipboard

fct_lump_prop() fix for empty levels

Open williamlai2 opened this issue 3 years ago • 0 comments

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

williamlai2 avatar Jun 18 '22 05:06 williamlai2