The following illustrates how to take a list column in a dataframe and wrangle it, thus making it easier to analyze. This is the is HTML output for the R Notebook, list_to_dataframe.Rmd and

From a Jenny Bryan Workshop but similar to Purrr tutorial: Food Markets in New York

• purrr – a package for iterating over lists
• repurrrsive – a package for learning purrr
library(purrr)
library(repurrrsive)
## The following are helpful tidyverse packages.
library(tibble)
library(dplyr)
library(tidyr)

Could have more simply loaded tidyverse & repurrrsive packages, but we’re learning here.

library(tidyverse)
library(repurrrsive)

## Assign onboard list got_chrs to an list sw_characters

sw_characters <- got_chars

## Simplify the list

select list items (name, and titles) to work on, transform them to a data frame with two variables. One variable is a character vector, the other variable is a list – because a single named character can have many alegiances (i.e. titles).

dataframe_list <- tibble(
name = map_chr(sw_characters, "name"),
title = map(sw_characters, "titles")
)
dataframe_list

## Transform

from nested list, to “unfilled” tall, then filldown (i.e. unnest())

dataframe_list %>%
mutate(n_titles = map_int(title, length)) %>%
filter(n_titles > 1) %>%
select(name, title) %>%
unnest()

## Simple List to Data Frame

If the list item is not a nested list, i.e. a single item in the list, then the following code works as peachy as a pie.

map_dfr(sw_characters, [, c("name", "gender"))