Values not included in levels

  • WARNING: Any values not in the level will be silently converted to NA:
y2 <- factor(x2, levels = month_levels)

## [1] Dec  Apr  <NA> Mar 
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  • But if you use forcats::fct() insted:
y2 <- fct(x2, levels = month_levels)

#> Error in fct(): #> ! All values of x must appear in levels or na #> ℹ Missing level: “Jam”

  • If you need to access the valid levels use levels():
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"