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:

e <- factor(c("high", "low", "medium", "low"))
# 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
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
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:
e <- factor(c("high", "low", "medium", "low"))
max(e) # throws an error
# ordered factor
e_ord <- factor(e, levels=c("low", "medium", "high"), ordered=TRUE)
max(e_ord) # outputs "high"