Get startedGet started for free

Renaming factor levels

1. Renaming factor levels

Sometimes, our qualitative variables don’t always come with the names we want. We may have names that make no sense or levels that we want to change to NA. In this case, we can use the forcats function fct_recode(), which allows us to rename the levels of our factors.

2. Introduction to FiveThirtyEight dataset

To see fct_recode() in action, let’s take a look at a dataset we’ll be using later in the course, 538’s survey on flying etiquette. This survey asked about 1,040 people various questions about their demographics, past behavior on flights, and opinions on different behaviors by fliers, like waking someone up to go for a walk. Let’s take a look at the responses offered for one question about who should get to use the two armrests in a row of three seats.

3. fct_recode()

As usual, our first step should be to look at what are the possible unique responses, so let’s use the function levels() to do so. We see that there are five possible answers, including "other." What happens when we try to make a frequency plot of this data with the following code?

4. Crowded text

Well, those x-axis labels certainly aren't readable! Each answer has too much text. We've seen before that we can flip the coordinates of a plot and sometimes that solves this problem. Let's try it.

5. Extraneous text

Well, we can now read what each answer is, but they probably don't need to be that long. Let's try renaming each level to something shorter but still understandable and changing the label for the y-axis to be a bit more descriptive.

6. Changing with fct_recode()

Here, we've renamed every level to something much shorter. The old label is on the right and our new ones are on the left.

7. Renaming a couple levels

We don't always have to rename every level; for example, we could have just renamed one of the levels, "the arm rests should be shared" and leave every other level the same.

8. Renaming unknown levels

What happens if we make a typo and try to change the name of a level that doesn't exist? The code will still run, but it won't change any levels and we'll get the warning of "unknown levels in f."

9. Let's practice!

Now it's your turn.