Factors

Here we are going to learn about R factors: how to create them and where they are used.

A Factor is an interesting data structure in R language used to categorize data. By categorizing data, we mean fields that take only predefined, a limited, or finite number of values (categorical variables).

For example, the marital status of a person can be one of the following:

  • Single
  • Married
  • Separated
  • Divorced
  • Widowed

Here we know that the possible values for marital status are 55. These values are predefined and distinct and are called levels.

Creating Factors

Factors can be created using the factor() function. This function takes all the levels in the form of a vector. Let’s dive right into the code.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy