Only this week, get the SQL Complete Track of 9 courses in a special prize of $330 $89!
Factors and how to create them
Working with factors
Modifying factor variables
16. Change factor levels


Suppose we want to change from the current five levels in statement to only three: Disagree, Neutral, Agree.

To do this, we need to lump all the Strongly Disagree and Somewhat Disagree responses into one response (Disagree). We can use the forcats fct_collapse() function to do this. It collapses the data from one or more levels into a new level. The arguments are the factor variable and the new level name. The levels that will be collapsed to form the new level are listed inside the new level, like this:

survey$simple_stmt <- fct_collapse(survey$statement,
  "Disagree" = c("Strongly Disagree", "Somewhat Disagree"))

Let's check the levels of this new factor:



Finish collapsing the answer in the statement column. Collapse Somewhat Agree and Strongly Agree to Agree. Assign the result to the simple_stmt column. Check the levels of the new factor.

Stuck? Here's a hint!


survey$simple_stmt <- fct_collapse(survey$statement,
  "Disagree" = c("Strongly Disagree", "Somewhat Disagree"),
  "Agree" = c("Strongly Agree", "Somewhat Agree"))