We discussed variable types in Chapter 1, Introduction to R, but did not include factor variables because they're a special type of variable in R that you must often create yourself. In this section, we'll learn what a factor variable is, when to use a factor variable, how to create one, what the levels of a factor are, and how to change the levels.
A factor variable in R is an explicitly declared categorical variable, or one that defines different categories or levels. Some common examples of factor variables include a variable describing sex, month, or one designating low/medium/high.
Recall our discussion of variable classes and types from Chapter 1, Introduction to R. A factor variable will always be of class factor, but can be any type: character, numeric, integer, or otherwise. For example, a variable indicating month can have the months as type...