Create a list-column data.frame
Let's end our chapter with an implementation of our links extractor, but using a list-column. The idea when using a nested dataframe (i.e., dataframe with a list column) is to keep everything inside a dataframe so that the workflow stays tidy.
You have been provided a tibble called df, which has a column urls with the four URLs you've been using since the beginning of this chapter. If you want to have a look at this dataframe, feel free to print it in the console.
We are going to create a new column called links, which contains the results of the get_links() function (available in your workspace). As the outputs of this function have different lengths, the output will be a list column that you will then need to unnest() to get back a standard dataframe.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
Load the three necessary packages:
dplyr,tidyr, andpurrrTake the
dfelement, and runmutate()on it.mutate()will map theget_links()function on theurlscolumn.Print the result.
Unnest the result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load dplyr, tidyr, and purrr
# Create a "links" columns, by mapping get_links() on urls
df2 <- df %>%
mutate(___ = map(___, get_links))
# Print df2 to see what it looks like
# unnest() df2 to have a tidy dataframe
df2 %>%
___(cols=c(links))