Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Factors and how to create them
4. Another way to create a factor
Working with factors
Modifying factor variables


As you probably guessed, the value that didn't match the defined levels was "secretly" converted to NA by R.

We would like to know about this before it happens! With a small vector, it's not much of a problem, but we don't often work with just small vectors. If we were using a large vector, maybe we wouldn't have noticed the change.

Fortunately, we have some help: we have forcats and the parse_factor() function.

The parse_factor() function can create a factor, but it also gives us a warning if the values in the factor do not match its levels. The advantage is that we know about all possible NAs before the factor is created. This is what it looks like:

fct_age <- parse_factor(c("senior", "adult"), levels = age_levels)

You can set the levels argument to NULL to tell R to deduce the factor levels from the unique values of the vector you passed in:

fct_age <- parse_factor(c("senior", "adult"), levels = NULL)


Use parse_factor() to create a new factor from the mrt_status vector. Use the marital_status_levels vector to create its levels. Assign the results to the fct_mrt_status variable.

Note: mrt_status contains a value, "separated", that is not found in marital_status_levels. What do you think will happen?

Stuck? Here's a hint!


fct_mrt_status <- parse_factor(
  levels = marital_status_levels)