Converting list of lists of dataframes to a single tibble

quantmod::getOptionChain( "SPY", NULL)

returns a list (dated) of two lists (calls, puts) of various sized dataframes.

I'm trying to convert this into a single (long) tibble, but due to the various sizes I can't use bind_rows and the only suggestion I see uses the obsolete plyr package.

What are my best options for doing this please, or do I need to write a processing loop? (I've looked at unnest_wider, but I can't quite figure out how to use it and retain all the data (for instance the dates of each of the top level lists.)

I'd try using data.table::rbindlist() with fill = TRUE.
Here's an hacky example:

mylist <- list(
  list(iris[,4:5], iris, iris),
  list(iris, iris[,2:5], iris[,1:2])
)
many_irises <- data.table::rbindlist(l = lapply(mylist, data.table::rbindlist, fill = TRUE), fill = TRUE)
dplyr::tibble(many_irises)

I think this is also an option:

bind_rows(flatten(mylist))

this is using

dplyr::bind_rows
purrr::flatten

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.