forcats icon indicating copy to clipboard operation
forcats copied to clipboard

Supporting NA for other_level in fct_collapse()

Open 1pakch opened this issue 4 years ago โ€ข 2 comments

IMO the fct_collapse() function should support using NA as the other_level argument.

As of 0.5.0 one cannot use neither NA to NA_character_

library(purrr)
fac <- c(1, 2, 3, 4) %>% factor()
forcats::fct_collapse(fac, A = c('1', '2'), other_level = NA_character_)
forcats::fct_collapse(fac, A = c('1', '2'), other_level = NA)

and the workaround is quite awkward and too long

(
    fac
    %>% forcats::fct_collapse(A = c('1', '2'), other_level = 'NA')
    %>% na_if('NA')
    %>% fct_drop()
)

I'd be happy to look into it and make a PR. This discussion might be relevant https://github.com/tidyverse/forcats/pull/101

1pakch avatar Dec 10 '21 13:12 1pakch

ๆ‚จๅฅฝ๏ผŒๆˆ‘ๅทฒๆ”ถๅˆฐๆ‚จ็š„้‚ฎไปถ๏ผŒๅฐ†ๅฐฝๅฟซ็ป™ๆ‚จๅ›žๅคใ€‚

xxspurs avatar Dec 10 '21 13:12 xxspurs

Reprex:

library(forcats)
x <- factor(1:4)
fct_collapse(x, A = c("1", "2"), other_level = "other")
#> [1] A     A     other other
#> Levels: A other
fct_collapse(x, A = c("1", "2"), other_level = NA)
#> Error in new[[other_level]] <- levels[!levels %in% levs]: attempt to select less than one element in integerOneIndex

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

hadley avatar Mar 02 '22 15:03 hadley

If we support this, also need to make sure it works in fct_lump_*() and fct_other().

fct_collapse() will need a decent amount of work to achieve this since it currently relies on fct_recode(). So maybe it make more sense to add a new fct_implicit_na() specifically for this purpose?

hadley avatar Jan 03 '23 20:01 hadley

Hmmm, rather surprisingly you can already do what you want with this:

library(forcats)
x <- factor(1:4)
fct_collapse(x, A = c("1", "2"), other_level = "NULL")
#> [1] A    A    <NA> <NA>
#> Levels: A

Created on 2023-01-03 with reprex v2.0.2

hadley avatar Jan 03 '23 21:01 hadley