rlevelsforcats

Collapse factor levels into missing levels


I would like to have "Dont know" and "Refuse" as missing, but assigning it to NULL does not seem to do the trick.

library(tidyverse)

mydata <- tibble(
  a = factor(c("Yes", "No", "Dont know", "Yes",  "Refuse", "No",
               "Dont know", "Yes", "No", "Dont know", "Refuse"))
)

mydata %>% 
  mutate(a = fct_collapse(a, NULL = c("Dont know", "Refuse"))) %>%
  count(a)

# # A tibble: 3 × 2
#   a         n
#   <fct> <int>
# 1 NULL      5
# 2 No        3
# 3 Yes       3

Solution

  • Since forcats v1.0.0, you should use fct_na_level_to_value:

    mydata %>% 
      mutate(a = fct_na_level_to_value(a, extra_levels = c("Dont know", "Refuse"))) %>%
      count(a)
    
    # # A tibble: 3 × 2
    #   a         n
    #   <fct> <int>
    # 1 No        3
    # 2 Yes       3
    # 3 NA        5