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

Load Packages

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"))
LS0tDQp0aXRsZTogImxpc3QgdG8gZGF0YWZyYW1lIg0KYXV0aG9yOiAgIkpvaG4gTGl0dGxlIg0KZGF0ZTogImByIFN5cy5EYXRlKClgIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQotLS0NCg0KDQpUaGUgZm9sbG93aW5nIGlsbHVzdHJhdGVzIGhvdyB0byB0YWtlIGEgbGlzdCBjb2x1bW4gaW4gYSBkYXRhZnJhbWUgYW5kIHdyYW5nbGUgaXQsIHRodXMgbWFraW5nIGl0IGVhc2llciB0byBhbmFseXplLiAgVGhpcyBpcyB0aGUgaXMgSFRNTCBvdXRwdXQgZm9yIHRoZSBSIE5vdGVib29rLCBbbGlzdF90b19kYXRhZnJhbWUuUm1kXShodHRwczovL2dpdGh1Yi5jb20vbGliam9obi9kYXRhZnJhbWVfd2l0aF9saXN0KSBhbmQgDQoNCkZyb20gYSBKZW5ueSBCcn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