A factor
is a special character
vector where the elements have pre-defined groups or ‘levels’. You can think of these as qualitative or categorical variables:
x <- c("red", "red", "blue", "yellow", "blue") class(x)
## [1] "character"
x_fact = factor(x) # factor() is a function class(x_fact)
## [1] "factor"
x_fact
## [1] red red blue yellow blue ## Levels: blue red yellow
Note that levels are, by default, in alphanumerical order.