R 从嵌套列表创建数据帧
从嵌套列表创建数据帧的最佳方式是什么R 从嵌套列表创建数据帧,r,R,从嵌套列表创建数据帧的最佳方式是什么 #installing package installed.packages("qdap") #loading qdap package library("qdap") # Finding synonyms from the below function which gives a list. data <- synonyms("power") # Converting list to dataframe plyr::ldply(data, cbi
#installing package
installed.packages("qdap")
#loading qdap package
library("qdap")
# Finding synonyms from the below function which gives a list.
data <- synonyms("power")
# Converting list to dataframe
plyr::ldply(data, cbind)
t(plyr::ldply(data, rbind))
使用dput()时,列表输出看起来很相似
这样行吗
lapply(l, function(li)
data.frame(lapply(li,'length<-',max(lengths(li)))))
# [[1]]
# power.def_1 power.def_2 power.def_3 power.def_4
# 1 ability brawn ascendancy authority
# 2 capability energy authority authorization
# 3 capacity force bottom licence
# 4 competence forcefulness command prerogative
# 5 competency intensity control privilege
# 6 faculty might dominance right
# 7 potential muscle domination warrant
# 8 <NA> potency dominion <NA>
# 9 <NA> strength influence <NA>
# 10 <NA> vigour mastery <NA>
# 11 <NA> weight rule <NA>
# 12 <NA> <NA> sovereignty <NA>
# 13 <NA> <NA> supremacy <NA>
# 14 <NA> <NA> sway <NA>
...
数据
l1 <- structure(list(power.def_1 = c("ability", "capability", "capacity",
"competence", "competency", "faculty", "potential"), power.def_2 = c("brawn",
"energy", "force", "forcefulness", "intensity", "might", "muscle",
"potency", "strength", "vigour", "weight"), power.def_3 = c("ascendancy",
"authority", "bottom", "command", "control", "dominance", "domination",
"dominion", "influence", "mastery", "rule", "sovereignty", "supremacy",
"sway"), power.def_4 = c("authority", "authorization", "licence",
"prerogative", "privilege", "right", "warrant")), .Names = c("power.def_1",
"power.def_2", "power.def_3", "power.def_4"))
l <- list(l1,l1)
l1请使用dput()
发布列表数据。希望帮助您的每个人都需要安装它,这使我无法回答类似的问题。好的,让我与您共享该结构…@PoGibas我已包含列表结构供您参考。提前谢谢你的帮助。好的。可能不会。如果您对输入和预期输出更清楚,那就最好了。谢谢您在这里的帮助,但是对于每个子列表,我想创建一个不同的数据框,而不是行绑定。很抱歉,如果一开始我不清楚。但是从您的解决方案中得到了一些提示。因此,如果您只是删除do.call(rbind,
您得到了您想要的吗?正确,并通过分配n个DataFrames在循环中运行嵌套列表我更新了我的答案以删除行绑定,但我不理解您的最后一条评论,无法安装qdap包。如果您需要进一步的帮助,请详细说明。
lapply(l, function(li)
data.frame(lapply(li,'length<-',max(lengths(li)))))
# [[1]]
# power.def_1 power.def_2 power.def_3 power.def_4
# 1 ability brawn ascendancy authority
# 2 capability energy authority authorization
# 3 capacity force bottom licence
# 4 competence forcefulness command prerogative
# 5 competency intensity control privilege
# 6 faculty might dominance right
# 7 potential muscle domination warrant
# 8 <NA> potency dominion <NA>
# 9 <NA> strength influence <NA>
# 10 <NA> vigour mastery <NA>
# 11 <NA> weight rule <NA>
# 12 <NA> <NA> sovereignty <NA>
# 13 <NA> <NA> supremacy <NA>
# 14 <NA> <NA> sway <NA>
...
library(purrr)
map(l,~map_dfc(.,`length<-`,max(lengths(.))))
l1 <- structure(list(power.def_1 = c("ability", "capability", "capacity",
"competence", "competency", "faculty", "potential"), power.def_2 = c("brawn",
"energy", "force", "forcefulness", "intensity", "might", "muscle",
"potency", "strength", "vigour", "weight"), power.def_3 = c("ascendancy",
"authority", "bottom", "command", "control", "dominance", "domination",
"dominion", "influence", "mastery", "rule", "sovereignty", "supremacy",
"sway"), power.def_4 = c("authority", "authorization", "licence",
"prerogative", "privilege", "right", "warrant")), .Names = c("power.def_1",
"power.def_2", "power.def_3", "power.def_4"))
l <- list(l1,l1)