从嵌套列表中包含的数据框中提取两列
Extracting two columns from a dataframe contained within a nested list
我从 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' 来自我的初始数据框到这些列,并将其绑定到一个新的数据框中。然后我会对这个数据框做一些额外的操作。
我已经想出如何从我的列表中提取其他项目。例如,我知道下面的代码会为列表列表中的每个项目提取 'points' 项目。
lapply(participants$lists, "[[", 'points')
我也知道以下代码将从列表第一项的 'pilots' 数据框中提取 'id' 列。
lists[[1]][['pilots']]['id']
但是,我不确定如何将子集化作为整个列表的函数来实现,我也不确定如何将“参与者”数据框中的标识符附加到这些项目。
从列表中的每个项目中提取整个 'pilots' 数据框并将其绑定在一起是行不通的,因为数据框的列数不同。我也试过将列表展平,但这似乎并没有让我到达我想去的地方,但也许我做错了。
do.call("rbind", lapply(participants$lists, "[[", 'pilots'))
感谢您提供的任何帮助。
我们通过选择 'id'、'ship' 列
从 list
和子集中提取“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")]
}
}))
如果我们想从 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)
我从 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' 来自我的初始数据框到这些列,并将其绑定到一个新的数据框中。然后我会对这个数据框做一些额外的操作。
我已经想出如何从我的列表中提取其他项目。例如,我知道下面的代码会为列表列表中的每个项目提取 'points' 项目。
lapply(participants$lists, "[[", 'points')
我也知道以下代码将从列表第一项的 'pilots' 数据框中提取 'id' 列。
lists[[1]][['pilots']]['id']
但是,我不确定如何将子集化作为整个列表的函数来实现,我也不确定如何将“参与者”数据框中的标识符附加到这些项目。
从列表中的每个项目中提取整个 'pilots' 数据框并将其绑定在一起是行不通的,因为数据框的列数不同。我也试过将列表展平,但这似乎并没有让我到达我想去的地方,但也许我做错了。
do.call("rbind", lapply(participants$lists, "[[", 'pilots'))
感谢您提供的任何帮助。
我们通过选择 'id'、'ship' 列
从list
和子集中提取“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")]
}
}))
如果我们想从 participants
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)