Exercise

Getting number of levels

With mutate() and summarize() in dplyr, you can use the across() function to specify to apply their second argument, a function, to all columns where the first argument is true.

We'll use these along with tidyr to get the number of levels for each factor variable in multiple_choice_responses. pivot_longer() from tidyr takes a dataset from wide to long format. Its two arguments are the new column names—one contain the old column names and one all the values.

Instructions

100 XP
  • Change all the character columns to factor columns and save the new dataset as responses_as_factors.
  • Create a new dataset, number_of_levels, where you:
    • Use summarize with across to apply the function nlevels() to each column.
    • Change the dataset's format from wide to long.