CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Categorical Data in the Tidyverse

Afficher le cours

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 with across to apply the function nlevels() to each column.
    • Change the dataset's format from wide to long.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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")
Modifier et exécuter le code