Factors and how to create them
2. Creating factors in base R
Working with factors
Modifying factor variables
Summary

Instruction

Our data will come from an online store's survey results. Before we dig into the data, we need to do some prep work.

We'll need to know the age category of our customers. There are three age categories: minor (under 18), adult (18–65) and senior (over 65). Let's make a vector containing the first two categories, or levels. We'll use it later in this section:

age_levels <- c("minor", "adult", "senior")

We want to create a factor. The base R function for this is factor(). We have the age vector, which looks like this:

age <- c("minor", "adult")

We can use factor() to create a new factor, fct_age, like this:

fct_age <- factor(age, levels = age_levels)

Exercise

Create a vector named marital_status_levels that contains the values "single", "married", and "divorced".

Use the mrt_status vector to create a factor, using the marital_status_levels vector as its levels. Assign it to the fct_mrt_status variable.

Stuck? Here's a hint!

Type:

marital_status_levels <- c("single", "married", "divorced")
mrt_status <- c("single", "divorced", "married", "single")
fct_mrt_status <- factor(mrt_status, levels = marital_status_levels)