我遇到下一个问题,试图将该列表列表转换为数据框,其中列表的唯一元素是其自己的列。
这就是我现在所拥有的:
> head(data$egg_groups)
[[1]]
name resource_uri
1 Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/
[[2]]
name resource_uri
1 Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/
[[3]]
name resource_uri
1 Plant /api/v1/egg/7/
2 Monster /api/v1/egg/1/
[[4]]
name resource_uri
1 Dragon /api/v1/egg/14/
2 Monster /api/v1/egg/1/
[[5]]
name resource_uri
1 Dragon /api/v1/egg/14/
2 Monster /api/v1/egg/1/
[[6]]
name resource_uri
1 Dragon /api/v1/egg/14/
2 Monster /api/v1/egg/1/
我想要的是一个数据框,其中的一项(只是名称)是其自己的一列。
像这样的东西:
Plant Monster Dragon
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
6 1 1
我已经尝试了图书馆plyr和使用方法unlist
,到目前为止没有任何效果。任何提示将不胜感激。谢谢
编辑:这是dput
pastebin链接:dput
我建议mtabulate
从“ qdapTools”包中使用。首先,只需遍历列表并将相关列提取为向量,然后将结果列表用作的输入mtabulate
,如下所示:
library(qdapTools)
head(mtabulate(lapply(L, `[[`, "name")))
# Bug Ditto Dragon Fairy Flying Ground Human-like Indeterminate Mineral Monster
# 1 0 0 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 0 1
# 3 0 0 0 0 0 0 0 0 0 1
# 4 0 0 1 0 0 0 0 0 0 1
# 5 0 0 1 0 0 0 0 0 0 1
# 6 0 0 1 0 0 0 0 0 0 1
# Plant Undiscovered Water1 Water2 Water3
# 1 1 0 0 0 0
# 2 1 0 0 0 0
# 3 1 0 0 0 0
# 4 0 0 0 0 0
# 5 0 0 0 0 0
# 6 0 0 0 0 0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句