名单列表到data.frame [英] list of named lists to data.frame
问题描述
我有一个来自JSON对象的以下表单的命名列表:
I have a list of named lists of the following form from a JSON object:
my_list = list(list(a = 10, b = "blah"),
list(a = 15, b = "stuff"))
外部列表的每个元素都是一个命名列表,我想将其转换为以下格式的data.frame,列名称完整无缺:
Each element of the outer list is a named list and I want to convert it to a data.frame of the following form with the column names intact:
a b
10 "blah"
15 "stuff"
表面上,我可以通过执行 to_df = data.frame(do.call(rbind,my_list))
来实现这一点。
On the surface, I can achieve this by doing to_df = data.frame(do.call(rbind, my_list))
.
但是,如果我尝试使用 to_df $ a
或 to_df [ 1]
我将得到一个列表,而不是一个从data.frame通常预期的向量:
However, if I were to try to extract an individual column using to_df$a
or to_df[,1]
I would get a list instead of a vector as normally expected from a data.frame:
> to_df[,1]
[[1]]
[1] 10
[[2]]
[1] 15
而不是:
> to_df[,1]
[1] 10 15
R邮件中的一个旧帖列表建议以下解决方案: to_df = as.data.frame(t(sapply(my_list,rbind)))
。但是,这不仅不会转移到列名称上,而是使用 to_df [,1]
查看各个列时仍然返回列表而不是向量的相同问题。 。
An old post on the R mailing list suggested the following solution: to_df = as.data.frame(t(sapply(my_list, rbind)))
. But not only does this not transfer over the column names, it still has the same issue of returning a list instead of a vector when looking at individual columns using to_df[,1]
.
达到此目的最好的方法是什么?有没有一个 dplyr
方式?
What's the best way to achieve this? Is there a dplyr
way?
编辑:感谢所有的解决方案,似乎诀窍是 lapply
,并将列表的每个元素转换为 data.frame
,然后使用dplyr或 do.call
。或者, data.table
大部分工作都是单次调用 rbindlist
。
Thanks for all the solutions, it appears the trick is to lapply
and transform each element of the list to a data.frame
and then bind them together using dplyr or do.call
. Alternatively, data.table
does most of the work with a single call to rbindlist
.
推荐答案
我喜欢 data.table 包中的 rbindlist
它很简单,快速,并返回一个数据框/表。
I prefer rbindlist
from the data.table package. It's simple, fast, and returns a data frame/table.
data.table::rbindlist(my_list)
# a b
# 1: 10 blah
# 2: 15 stuff
另一个 rbindlist()
的优点是,它将自动填入缺少的值,其中 NA
。
Another advantage of rbindlist()
is that it will automatically fill in missing values with NA
.
要删除 data.table
类,只需将 as.data.frame()
as.data.frame(data.table::rbindlist(my_list))
这篇关于名单列表到data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!