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 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: