Next →

Output:


      

Plots:

Factors in R

Factors are used to represent categorical data. They store both the values and the possible levels, which makes them useful for grouping and statistical modeling.

Creating Factors

gender <- factor(c("Male", "Female", "Female", "Male"))
levels(gender)

You can also specify the order of levels:

education <- factor(c("Bachelor", "Master", "PhD", "Bachelor"),
                    levels = c("Bachelor", "Master", "PhD"),
                    ordered = TRUE)

Why Use Factors?