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(c("high", "low", "medium", "low"))
e # check the structure of e
str(e)
## Factor w/ 3 levels "high","low","medium": 1 2 3 2
- Example of a character vector versus a factor
# factor
<- factor(c("high", "low", "medium", "low"))
e # character vector
<- c("high", "low", "medium", "low")
e2 # Check the structure of both objects
str(e)
## Factor w/ 3 levels "high","low","medium": 1 2 3 2
str(e2)
## 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:
# unordered factor:
<- factor(c("high", "low", "medium", "low"))
e max(e) # throws an error
# ordered factor
<- factor(e, levels=c("low", "medium", "high"), ordered=TRUE)
e_ord max(e_ord) # outputs "high"