9.4 Factors
A factor is a vector object (1 dimension) used to specify a discrete classification (grouping) of the components of other vectors.
Factors are mainly used for statistical modeling, and can also be useful for graphing.
You can create factors with the factor function, for example:
## Factor w/ 3 levels "high","low","medium": 1 2 3 2
- Example of a character vector versus a factor
# factor
e <- factor(c("high", "low", "medium", "low"))
# character vector
e2 <- c("high", "low", "medium", "low")
# Check the structure of both objects
str(e)
## Factor w/ 3 levels "high","low","medium": 1 2 3 2
## chr [1:4] "high" "low" "medium" "low"
- Groups in factors are called levels.
Levels can be ordered. Then, some operations applied on numeric vectors can be used: