IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Categorical Data in the Tidyverse

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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")
Modifica ed esegui il codice