Chapter 8 Factors with forcats

library(tidyverse)
library(forcats)

#creating factors
(x1 <- c("Dec", "Apr", "Jan", "Mar"))
## [1] "Dec" "Apr" "Jan" "Mar"
#two possible issues
(x2 <- c("Dec", "Apr", "Jam", "Mar"))
## [1] "Dec" "Apr" "Jam" "Mar"
sort(x1)
## [1] "Apr" "Dec" "Jan" "Mar"
(month_levels <- c(
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
))
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
y1 <- factor(x1, levels = month_levels)
y1
## [1] Dec Apr Jan Mar
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
sort(y1)
## [1] Jan Mar Apr Dec
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
y2 <- factor(x2, levels = month_levels)
y2
## [1] Dec  Apr  <NA> Mar 
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#diagnose typos
y2 <- parse_factor(x2, levels = month_levels)
## Warning: 1 parsing failure.
## row col           expected actual
##   3  -- value in level set    Jam
y2
## [1] Dec  Apr  <NA> Mar 
## attr(,"problems")
## # A tibble: 1 x 4
##     row   col expected           actual
##   <int> <int> <chr>              <chr> 
## 1     3    NA value in level set Jam   
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#levels statement
factor(x1)
## [1] Dec Apr Jan Mar
## Levels: Apr Dec Jan Mar
f1 <- factor(x1, levels = unique(x1))
f1
## [1] Dec Apr Jan Mar
## Levels: Dec Apr Jan Mar
f2 <- x1 %>% factor()
f2
## [1] Dec Apr Jan Mar
## Levels: Apr Dec Jan Mar
levels(f2)
## [1] "Apr" "Dec" "Jan" "Mar"