Fix issues with strings using factors
- Fix those problems with a factor.
- Start by creating a list of the valid levels:
month_levels <- c(
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
)
month_levels
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
y1 <- factor(x1, levels = month_levels)
sort(y1)
## [1] Jan Mar Apr Dec
## Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
- If you omit the levels, they’ll be taken from the data alphabetically:
## [1] Dec Apr Jan Mar
## Levels: Apr Dec Jan Mar