For a single list of data.frames objects, I'd usually have no trouble converting that:
my_df <- do.call("rbind", lapply(my_list, data.frame))
However, the list object I currently have is nested. It is a list of lists of data.frames. A few points to note:
- The elements of some child lists within the parent list are empty.
- Among child lists with information, some lists have more than one data.frame object.
- The number of data.frame objects can vary among child lists.
Here's a simplified example of what I'm dealing with:
List of 3
$ 1 :List of 2
..$ : NULL
..$ : NULL
$ 2 :List of 2
..$ :'data.frame': 3 obs. of 2 variables:
.. ..$ name : chr [1:3] "jack" "jim" "joe" "jon"
.. ..$ value : chr [1:3] "10" "12" "13" "14"
..$ :'data.frame': 4 obs. of 2 variables:
.. ..$ name : chr [1:4] "jacky" "jane" "juanita" "julia"
.. ..$ value : chr [1:4] "11" "9" "10" "14"
$ 3 :List of 1
..$ :'data.frame': 5 obs. of 2 variables:
.. ..$ name : chr [1:5] "adam" "ashley" "arnold" "avery" "arthur"
.. ..$ value : chr [1:5] "6" "7" "11" "12" "11"
The do.call
approach above reports an error that arguments imply differing number of rows
, so it seems like my lists with data.frames with different row numbers is causing the issue?
I tried some strategies described in this post but each attempt had its own unique error.
The data.table
"rbindlist" approach and dplyr
"bind_rows" methods both reported:
fill=TRUE, but names of input list at position 1 is NULL
Thanks for any tips on how to deal with the situation.