How to create a data frame out of a deeply nested list of lists that contains data frames as well?

Keywords: r list dataframe nested rstudio

Question: 

I work with data from TMDb that I got with help from the API and R. The output is one list containing n-lists that contain data frames themselves. I can't work with nested lists and prefer just one data frame.

It feels like I've tried everything, but nothing works.

Reproducable Code for my ugly list (imagine this list has over 400.000 lists itself):

genres <- data.frame("id" = as.integer(c(35, 40)), "name" = c('Horror','Comedy'), stringsAsFactors = FALSE)

cast <- data.frame("id" = as.integer(c(1, 2)), "name" = c("Bruce Willis", "Demi Moore"), stringsAsFactors = FALSE)

crew <- data.frame("job" = c("Director", "Producer", "Screenwriter"), "id" = as.integer(c(1,2,3)), "name" = c("Steven Spielberg", "Peter Pan", "Arnold Schwarzenegger"), "gender" = as.integer(c(0,0,0)), stringsAsFactors = FALSE)

genres2 <- data.frame("id" = as.integer(c(42, 43)), "name" = c("Drama","Lovestory"), stringsAsFactors = FALSE)

cast2 <- data.frame("id" = as.integer(c(3, 4)), "name" = c("Johnny Depp", "Leonardo Di Caprio"), stringsAsFactors = FALSE)

crew2 <- data.frame("job" = c("Director", "Producer", "Producer", "Screenwriter"),"id" = as.integer(c(1,6,7,8)), "name" = c("Steven Spielberg", "Dumbo", "Cinderella", "Micky Mouse"), "gender" = as.integer(c(0,0,1,0)), stringsAsFactors = FALSE)

genres3 <- data.frame("id" = as.integer(c(35, 42)), "name" = c("Horror","Drama"), stringsAsFactors = FALSE)


list <- list(list("id" = as.integer(601), 
                  "revenue" = as.integer(15000), 
                  "genre" = genres, 
                  "cast" = list("cast" = cast, "crew" = crew)), 
             list("id" = as.integer(602), 
                  "revenue" = as.integer(20000), 
                  "genre" = genres2, 
                  "cast" = list("cast" = cast2, "crew" = crew2)),
             list("id" = as.integer(603), 
                  "revenue" = as.integer(12000), 
                  "genre" = genres3, 
                  "cast" = list("cast" = list(), "crew" = list())))                     

I just want to flatten this nested list and get 3 data frames out of it. I'm only interested in Producer and Director.

df1

id    revenue   genre               Producer            Director
601   15000     Horror, Comedy      Peter Pan           Steven Spielberg
602   20000     Drama, Lovestory    Dumbo, Cinderella   Steven Spielberg

df2 - So here I need the collaborations from Producer and Director

id    Producer    Director
601   Peter Pan   Steven Spielberg
602   Dumbo       Steven Spielberg
602   Cinderella  Steven Spielberg

df3 - Only information about people

Name              Gender    ID
Peter Pan         0         2 
Steven Spielberg  0         1
Cinderella        1         7
.
.
.

I hope you can help me in any kind of way. I hope it is understandable.

Answers: