我有一个列表,其中每个元素都是命名列表,但元素在各处并不相同。我已阅读有关如何将列表列表转换为数据帧的解决方案here https://stackoverflow.com/questions/4512465/what-is-the-most-efficient-way-to-cast-a-list-as-a-data-frame?rq=1 and here https://stackoverflow.com/questions/4227223/r-list-to-data-frame,但是当列表不相同时,这些都不起作用。
示例 - 请注意,我也有混合类型,如果解决方案将所有内容强制为字符,那就没问题了。
lisnotOK <- list(list(a=1, b=2, c="hi"), list(b=2, c="hello", d="nope"))
结果应该简单地具有 NA,其中列不能由列表填充,就像rbind.fill
来自 plyr,或rbind_all
来自 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
任何使用的解决方案rbind
,或试图使lisnotOK
进入矩阵将会失败,而上面链接的帖子中的任何示例都不起作用,即使我尝试使用rbind_all
or rbind.fill
.
一种解决方案是一个丑陋的 for 循环,其中每个连续列表都更改为数据帧,并使用rbind_all
绑定到数据框。
有谁知道有效的解决方案?