CommencerCommencer gratuitement

Expecting mapped output

When you know that the output of your mapped function is an expected type (here it is a numeric vector) you can leverage the map_*() family of functions to explicitly try to return that object type instead of a list.

Here you will again calculate the mean population of each country, but instead, you will use map_dbl() to explicitly append the numeric vector returned by mean() to your data frame.

Cet exercice fait partie du cours

Machine Learning in the Tidyverse

Afficher le cours

Instructions

  • Generate the pop_mean data frame using the map_dbl() function to calculate the population mean for each nested data frame.
  • Explore the pop_mean data frame using head().

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Calculate mean population and store result as a double
pop_mean <- gap_nested %>%
  mutate(mean_pop = map_dbl(___, ~mean(.x$___)))

# Take a look at pop_mean
head(___)
Modifier et exécuter le code