Search code examples
rdataframenested-listsrbind

Converting nested list to dataframe


The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is:

mylist <- list(
  list(
    Hit = "True",
    Project = "Blue",
    Year = "2011",
    Rating = "4",
    Launch = "26 Jan 2012",
    ID = "19",
    Dept = "1, 2, 4"
  ),
  list(Hit = "False", Error = "Record not found"),
  list(
    Hit = "True",
    Project = "Green",
    Year = "2004",
    Rating = "8",
    Launch = "29 Feb 2004",
    ID = "183",
    Dept = "6, 8"
  )
)

When there are no missing records the list can be converted into a data frame using data.frame(do.call(rbind.data.frame, mylist)). However, when records are missing this results in a column mismatch. I know there are functions to merge data frames of non-matching columns but I'm yet to find one that can be applied to lists. The ideal outcome would keep record 2 with NA for all variables. Hoping for some help.


Solution

  • You can also use (at least v1.9.3) of rbindlist in the data.table package:

    library(data.table)
    
    rbindlist(mylist, fill=TRUE)
    
    ##      Hit Project Year Rating      Launch  ID    Dept            Error
    ## 1:  True    Blue 2011      4 26 Jan 2012  19 1, 2, 4               NA
    ## 2: False      NA   NA     NA          NA  NA      NA Record not found
    ## 3:  True   Green 2004      8 29 Feb 2004 183    6, 8               NA