6 Factors

6.1 Factors

A factor is a data type similar to a vector. However, the values contained in a factor can only be selected from a set of levels.

## [1] "Bungalow" "Flat"     "Flat"     "Detached" "Flat"     "Terrace" 
## [7] "Terrace"
## [1] Bungalow Flat     Flat     Detached Flat     Terrace  Terrace 
## Levels: Bungalow Detached Flat Terrace

6.2 table

The function table can be used to obtain a tabulated count for each level.

## [1] Bungalow Flat     Flat     Detached Flat     Terrace  Terrace 
## Levels: Bungalow Detached Flat Terrace
## houses_factor
## Bungalow Detached     Flat  Terrace 
##        1        1        3        2

6.3 Specified levels

A specific set of levels can be specified when creating a factor by providing a levels argument.

## houses_factor_spec
## Bungalow     Flat Detached     Semi  Terrace 
##        0        3        0        0        2

6.4 (Unordered) Factors

In statistics terminology, (unordered) factors are categorical (i.e., binary or nominal) variables. Levels are not ordered.

## Warning in Ops.factor(income_nominal, "Low"): '>' not meaningful for
## factors
## [1] NA NA NA NA NA NA NA NA

6.5 Ordered Factors

In statistics terminology, ordered factors are ordinal variables. Levels are ordered.

## [1]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
## [1] Low    Low    Low    Low    Medium Medium High   High  
## Levels: Low < Medium < High