Mapping your data
In combination with mutate(),
you can use map()
to append the results of your calculation to a data frame. Since the map()
function always returns a vector of lists you must use unnest()
to extract this information into a numeric vector.
Here you will explore this functionality by calculating the mean population of each country in the gapminder
dataset.
Diese Übung ist Teil des Kurses
Machine Learning in the Tidyverse
Anleitung zur Übung
- Use
map()
to apply themean()
function to calculate the population mean for each country and append this new list column calledmean_pop
usingmutate()
. - Explore the first 6 rows of
pop_nested
. - Use
unnest()
to convert themean_pop
list into a numeric column and save this as thepop_mean
data frame. - Explore
pop_mean
usinghead()
.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Calculate the mean population for each country
pop_nested <- gap_nested %>%
mutate(mean_pop = map(___, ~mean(.x$___)))
# Take a look at pop_nested
head(___)
# Extract the mean_pop value by using unnest
pop_mean <- pop_nested %>%
unnest(___)
# Take a look at pop_mean
head(___)