Get startedGet started for free

Changing characters to factors

While we'd sometimes like to dive straight into data visualization and modeling, we usually need to do some data tidying first. This chapter will walk you through the steps needed to recreate a FiveThirtyEight graphic from the original dataset, flying_etiquette. We'll start by limiting our dataset to only the respondents we're interested in and making some of our columns factors.

In the code, you'll see that we've put backticks around the column name. This is what you need to do if there are spaces in your column names! Normally, we change those to underscores so we don't have to do this, but in this case, we've kept it since we'll soon be graphing them.

This exercise is part of the course

Categorical Data in the Tidyverse

View Course

Exercise instructions

  • Change all character columns into factor columns.
  • Remove people who responded "Never" to a question asking if they have flown before.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

flying_etiquette %>%
    # Change characters to factors
    mutate(across(___(___), as.factor)) %>%
    # Filter out those who have never flown on a plane
    ___(`How often do you travel by plane?` != ___)
Edit and Run Code