tidyverse / forcats

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

`fct_lump_prop()` errors with weights and unused levels #292

Closed sam-crawley closed 1 year ago

sam-crawley commented 2 years ago

When calling fct_lump_prop with weights, and with a factor that has unused levels, I get the error:

Error in if (prop > 0 && sum(prop_n <= prop) <= 1) { : missing value where TRUE/FALSE needed

Reprex:

f <- factor(c("a", "b", "c"), levels = c("a", "b", "c", "d"))
f <- fct_lump_prop(f, prop = 0.02,  w = c(1,1,1))

Calling fct_drop() beforehand prevents the error.

hadley commented 2 years ago

Reprex:

library(forcats)
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))
#> Error in if (prop > 0 && sum(prop_n <= prop) <= 1) {: missing value where TRUE/FALSE needed

Created on 2022-03-02 by the reprex package (v2.0.1)