I have a list where each element is a named list, but the elements are not the same everywhere. I have read solutions on how to convert lists of lists to dataframes here and here, but none of this works when the lists are not identical.
Example - note I have mixed types as well, it is fine if the solution coerces everything to character.
lisnotOK <- list(list(a=1, b=2, c="hi"), list(b=2, c="hello", d="nope"))
The result should simply have NA where a column cannot be filled by a list, just like rbind.fill
from plyr, or rbind_all
from dplyr.
Example
lisOK <- list(list(a=1, b=2, c="hi"), list(a=3, b=5, c="bye"))
# One of many solutions
do.call(rbind.data.frame, lisOK)
# gives
a b c
2 1 2 hi
21 3 5 bye
Any solution that uses rbind
, or tries to make lisnotOK
into a matrix will fail, whereas any examples in the posts linked above don't work, even when I try to use rbind_all
or rbind.fill
.
One solution is an ugly for loop where each successive list is changed to a dataframe, and uses rbind_all
to bind to a dataframe.
Does anyone know an efficient solution?