Factors store categorical values with known levels. Ordered factors keep category order separate from alphabetical order.

Program

Play the script to watch text priorities become an ordered factor and then return to text.

factors.R
levels <- c("low", "medium", "high")
priority <- factor(c("high", "low"), levels = levels, ordered = TRUE)
first_level <- as.character(priority[1])
cat(first_level, "\n", sep = "")
factor `factor` stores category values with a fixed level set.
ordered factor `ordered = TRUE` gives the levels a meaningful order.
as.character `as.character` converts factor values back to text labels.