【问题标题】:Extracting two columns from a dataframe contained within a nested list从嵌套列表中包含的数据框中提取两列
【发布时间】:2021-01-24 18:23:52
【问题描述】:

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

Image of Dataframe

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

[[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']

但是,我不确定如何将该子集实现为整个列表的函数,并且我不确定如何将“参与者”数据框中的标识符附加到这些项目。

从列表中的每个项目中提取整个“飞行员”数据框并将其绑定在一起是行不通的,因为数据框的列数不同。我也尝试过扁平化列表,但这似乎并没有让我到达我想去的地方,但也许我做错了。

do.call("rbind", lapply(participants$lists, "[[", 'pilots'))

感谢您提供的任何帮助。

【问题讨论】:

    标签: r dataframe lapply nested-lists


    【解决方案1】:

    我们从list 和子集中提取“pilots”元素,方法是选择“id”、“ship”列

    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")]
                   }
                 }))
    

    如果我们想添加来自participants的'id'

    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)
    

    【讨论】:

    • 感谢您的回复。这给了我以下错误:[.data.frame(x$pilots, c("id", "ship")) 中的错误:选择了未定义的列这是回溯:6.stop("undefined columns selected") 5.@ 987654328@(x$pilots, c("id", "ship")) 4. x$pilots[c("id", "ship")] 3. FUN(X[[i]], ...) 2. lapply(participants$lists, function(x) x$pilots[c("id", "ship")]) 1. do.call(rbind, lapply(participants$lists, function(x) x$pilots[ c("id", "ship")]
    • @sethltaylor 你在“飞行员”的所有元素中都有“id”和“ship”吗?即是否有可能某些 data.frame 没有该列?
    • 如果我使用 flatten() 将列表展平一次(称为 'lists_flat'),然后 lists_flat$pilots[c('id', 'ship') 会从第一个'飞行员的数据框,但 lapply(lists_flat, function(x) x$pilots[c('id', 'ship')]) 给了我“x$pilots 中的错误:$ 运算符对原子向量无效”
    • @sethltaylor 你能试试do.call(rbind, lapply(do.call(c, lapply(participants$lists, function(x) x$pilots)), subset, select = c("id", "ship")))
    • 是的,必须是其中一个数据框没有该列的情况。我将我的列表子集为 5 个元素,我可以验证它们并且您的解决方案完美运行。有没有办法从没有这些列的列表中删除数据框?对于如何将我的参与者 ID 从整个数据框中附加到飞行员和船舶的新数据框中的正确行,您有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2019-09-09
    • 2023-04-06
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多