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


In this part, we learned how to work with factors using forcats as well as some base R functions. We discovered how to create factors using parse_factor():

parse_factor(mrt_status, levels = marital_status_levels)

and how to check factor levels with levels(). And we also learned how to order and reorder factor levels using fct_infreq() and fct_rev():

survey$age <- fct_rev(fct_infreq(survey$age))

Below is a summary of the useful ways of working with factors:

  • fct_expand() adds new levels to an existing factor:

    fct_expand(fct_mrt_status, "separated")
  • fct_explicit_na() gives missing values a factor level:

  • fct_recode() creates new levels and assigns existing levels to them:

      "Disagree" = "Strongly Disagree",
      "Disagree" = "Somewhat Disagree")
  • fct_collapse() collapses data from one or more levels to a new level:

      "Disagree" = c("Strongly Disagree", "Somewhat Disagree"))
  • fct_lump() creates an "Other" category and assign certain levels to it:

    fct_lump(survey$primary_language, n = 5)

We've seen a lot of these functions in action. Let's practice some before we move on to the next part.

Data from an Internet survey about car brands and customers' satisfaction has been loaded into memory for you. Let's look into this data.


Familiarize yourself with the survey data. Display the first six rows of the cars dataset. Use the head() function.

Stuck? Here's a hint!