R 从嵌套列表中包含的数据帧中提取两列

R 从嵌套列表中包含的数据帧中提取两列,r,dataframe,lapply,nested-lists,R,Dataframe,Lapply,Nested Lists,我从一个API中提取数据并将其从JSON转换而来,最终得到了一个游戏中锦标赛信息的数据框(“参与者”),其中包含一个标识比赛参与者的标识符(“id”),然后是一个包含参与者所玩游戏信息的嵌套列表。在这个嵌套列表中有一个数据框,其中包含我感兴趣的列。这是我的数据框中相关列的图像 例如,这是列表中的前两项 [[1]] [[1]]$description [1] "" [[1]]$faction [1] "galacticempire" [[1]]$nam

我从一个API中提取数据并将其从JSON转换而来,最终得到了一个游戏中锦标赛信息的数据框(“参与者”),其中包含一个标识比赛参与者的标识符(“id”),然后是一个包含参与者所玩游戏信息的嵌套列表。在这个嵌套列表中有一个数据框,其中包含我感兴趣的列。这是我的数据框中相关列的图像

例如,这是列表中的前两项

[[1]]
[[1]]$description
[1] ""

[[1]]$faction
[1] "galacticempire"

[[1]]$name
[1] "Unnamed Squadron"

[[1]]$pilots
          id       name points           ship upgrades.force-power   upgrades.sensor upgrades.modification upgrades.talent upgrades.gunner
1 darthvader darthvader     84  tieadvancedx1                 hate firecontrolsystem          afterburners            NULL            NULL
2 soontirfel soontirfel     54 tieinterceptor                 NULL              NULL                  NULL        predator            NULL
3 puresabacc puresabacc     62   tieskstriker                 NULL              NULL                  NULL     outmaneuver    fifthbrother

[[1]]$points
[1] 200

[[1]]$vendor
[[1]]$vendor$yasb
[[1]]$vendor$yasb$builder
[1] "Yet Another Squad Builder 2.0"

[[1]]$vendor$yasb$builder_url
[1] "https://raithos.github.io/"

[[1]]$vendor$yasb$link
[1] "https://raithos.github.io/?f=Galactic%20Empire&d=v5!s!173:204,113,-1,105:;179:127,-1,-1:;210:126,82,-1,-1:&sn=Unnamed%20Squadron&obs="



[[1]]$version
[1] "2.0.0"


[[2]]
[[2]]$name
[1] "Adam"

[[2]]$faction
[1] "scumandvillainy"

[[2]]$favourite
[1] TRUE

[[2]]$pilots
            ship upgrades.talent upgrades.crew upgrades.sensor upgrades.title        id points
1    fangfighter        fearless          NULL            NULL           NULL   fennrau     71
2    fangfighter        fearless          NULL            NULL           NULL oldteroch     59
3 g1astarfighter       trickshot           000 advancedsensors     misthunter      4lom     63

[[2]]$format
[1] "Extended"

[[2]]$version
[1] "2.3.5"

[[2]]$points
[1] 193
对于列表中的每个项目,我感兴趣的是从列表中的$pilots项目中提取'id'列和'ship'列,将初始数据框中的'id'附加到这些列中,并将其绑定到新的数据框中。然后我会在这个数据帧上做一些额外的操作

我已经知道如何从我的列表中提取其他项目。例如,我知道下面的代码为列表列表中的每个项目提取“点”项目

lapply(participants$lists, "[[", 'points')
我还知道下面的代码将从列表第一项中的“pilots”数据帧中提取“id”列

lists[[1]][['pilots']]['id']
但是,我不确定如何在整个列表中实现该子集作为一个函数,也不确定如何将“参与者”数据框中的标识符附加到这些项中

从列表中的每个项目中拉出整个“pilots”数据帧并将其绑定在一起是不起作用的,因为数据帧的列数不同。我也尝试过将列表展平,但这似乎并没有让我达到我想去的地方,但也许我只是做得不对

do.call("rbind", lapply(participants$lists, "[[", 'pilots'))
感谢您提供的帮助。

我们通过选择“id”、“ship”列,从
列表和子集中提取“pilots”元素

do.call(rbind, lapply(participants$lists, function(x) x$pilots[c("id", "ship")]))

如果“pilots”中有两列都不包含的元素,并且希望删除这些元素,那么使用
If
条件,我们可以这样做

do.call(rbind, lapply(participants$lists, function(x) {
              x1 <- x$pilots
              if(all(c("id", "ship") %in% names(x1))) {
                x1[c("id", "ship")]
               }
             }))

感谢您的回复。这给了我以下错误:
[.data.frame
(x$pilots,c(“id”,“ship”)):未定义的列被选中这是回溯:6.停止(“未定义的列被选中”)5.
[.data.frame
(x$pilots,c(“id”,“ship”))4.x$pilots[c(“id”,“ship”)]3.乐趣(x[[i],…)2.lappy(参与者$lists,函数(x)x$pilots[c(“id”,“ship”)])1.do.call(rbind,lappy(参与者$lists,函数(x)x$pilots[c(“id”,“ship”)])@sethltaylor在“pilots”的所有元素中,你是否都有“id”,“ship”?也就是说,是否有可能某些data.frame没有列?如果我使用flatte()将列表展平(称之为'lists_flat')然后列出'u flat$pilots[c('id','ship')给我第一个'pilots'数据帧中我想要的列,但是lappy(lists_flat,function(x)x$pilots[c('id','ship'))给我“x$pilots中的错误:$operator对原子向量无效”@sethltaylor你能试试
do.call(rbind,lappy)(do.call,c,lappy)吗(参与者$lists,函数(x)x$pilots)),子集,选择=c(“id”,“ship”))
是的,必须是其中一个数据帧没有该列。我将我的列表子集为5个元素,我可以验证它是否工作正常,您的解决方案是否工作正常。有没有办法从没有这些列的列表中删除数据帧?您对如何从整个数据帧中附加我的参与者id有什么建议在飞行员和船只的新数据框中,ame是否为正确的行?
lst1 <- lapply(participants$lists, function(x) {
              x1 <- x$pilots
              if(all(c("id", "ship") %in% names(x1))) {
                x1[c("id", "ship")]
               }
             })
i1 <- sapply(lst1, NROW) > 0
lst1[i1] <- Map(cbind, id2 = participants$id[i1], lst1[i1])
out <- do.call(rbind, lst1)