Factors and how to create them
Working with factors
Modifying factor variables
16. Change factor levels
Summary

Instruction

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:

levels(survey$simple_stmt)

Exercise

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!

Type:

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