Factor Variables

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

Get R Programming Fundamentals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.