Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Categorical Data in the Tidyverse

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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?` != ___)
Code bewerken en uitvoeren