Understanding Factors

Learn how to cast character strings or numbers as factors so that they are treated as categories.

This tutorial will teach you the basics of R. It is not necessary to have any prior programming experience.

Factors are objects which allow us to treat categorical data, so that it’s not incorrectly interpreted in the program as a number or a string of text. Factors are categories or levels.

If we have a look at the structure of our “animals” data frame we created in the video on vectors, we can see it includes three vectors here:

  • “animals”

  • “weight”

  • “class.tag”

If we look closely at “class.tag”, here we can see that this has been treated as an integer number when it is in fact a class represented as a number. We need to turn it into a factor. So that one, two, and three are factor levels or class labels. We can simply do this using the “as.factor” function or the “factor” function in R.

To get started watch the video

That’s all the information you need to understand factors in R. Also, check out our Data Science Bootcamp that can help you reinforce this skill. In the next video, we’ll look at calling pre-built functions in R.