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.
This exercise is part of the course
Categorical Data in the Tidyverse
Exercise instructions
- 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
withacross
to apply the functionnlevels()
to each column. - Change the dataset's format from wide to long.
- Use
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Change all the character columns to factors
responses_as_factors <- multiple_choice_responses %>%
mutate(___(is.character, as.factor))
number_of_levels <- responses_as_factors %>%
# Apply the function nlevels to each column
summarize(___(everything(), ___)) %>%
# Change the dataset from wide to long
___(everything(), names_to = "variable", values_to = "num_levels")