R将嵌套列表的元素写入csv
R write elements of nested list to csv
我有一个列表列表,由于某些 JSON 文件的结构,这些列表又包含多个列表。每个列表都有相同的数字(即 48 个列表,1 个列表,1 个列表,1 个列表,2 个列表 [我需要最后两个列表中的第一个])。现在,问题是,我需要从所有这些列表中检索深度嵌套的数据。
一个可重现的例子。
列表结构大致如下(可能多一层):
list1 = list(speech1 = 1, speech2 = 2)
list2 = list(list1, randomvariable="rando")
list3 = list(list2) #container
list4 = list(list3, name="name", stage="stage")
list5 = list(list4) #container
list6 = list(list5, date="date")
listmain1 = list(list6)
listmain2 = list(list6)
listmain3 = list(listmain1, listmain2)
结构应该是这样的:
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2
[[1]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"
[[1]][[1]][[1]][[1]]$name
[1] "name"
[[1]][[1]][[1]][[1]]$stage
[1] "stage"
[[1]][[1]]$date
[1] "date"
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[[2]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2
[[2]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"
[[2]][[1]][[1]][[1]]$name
[1] "name"
[[2]][[1]][[1]][[1]]$stage
[1] "stage"
[[2]][[1]]$date
[1] "date"
最终结果如下所示:
date name speech1 speech2
1
2
我想用我需要的变量创建列,并从我从中提取它们的列表中创建行。在上面的示例中,我需要从所有主列表中检索变量 speech1、speech2、名称和日期,并转换为更简单的数据框。我不太确定执行此操作的最快方法,并且在过去的几天里一直在用 lapply() 和 purrr 敲我的头。理想情况下,我想将列表视为列中具有扁平化变量的 rowID——但这也很棘手。任何帮助表示赞赏。
通过遍历每个列表,将其展平并获取值,可以使用 base R 快速实现:
# Your data
list1 = list(speech1 = 1, speech2 = 2)
list2 = list(list1, randomvariable="rando")
list3 = list(list2) #container
list4 = list(list3, name="name", stage="stage")
list5 = list(list4) #container
list6 = list(list5, date="date")
listmain1 = list(list6)
listmain2 = list(list6)
listmain3 = list(listmain1, listmain2)
# Loop over each list inside listmain3
flatten_list <- lapply(listmain3, function(x) {
# Flatten the list and extract the values that
# you're interested in
unlist(x)[c("date", "name", "speech1", "speech2")]
})
# bind each separate listo into a data frame
as.data.frame(do.call(rbind, flatten_list))
#> date name speech1 speech2
#> 1 date name 1 2
#> 2 date name 1 2
除非您想将行名称映射到每个列表中的某些值,否则行名称的顺序应与列表的数量相同。也就是说,如果您在 48 个嵌套列表上 运行,行名称将下降到 1:48
,因此无需使用 row.names
参数。
我有一个列表列表,由于某些 JSON 文件的结构,这些列表又包含多个列表。每个列表都有相同的数字(即 48 个列表,1 个列表,1 个列表,1 个列表,2 个列表 [我需要最后两个列表中的第一个])。现在,问题是,我需要从所有这些列表中检索深度嵌套的数据。
一个可重现的例子。
列表结构大致如下(可能多一层):
list1 = list(speech1 = 1, speech2 = 2)
list2 = list(list1, randomvariable="rando")
list3 = list(list2) #container
list4 = list(list3, name="name", stage="stage")
list5 = list(list4) #container
list6 = list(list5, date="date")
listmain1 = list(list6)
listmain2 = list(list6)
listmain3 = list(listmain1, listmain2)
结构应该是这样的:
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2
[[1]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"
[[1]][[1]][[1]][[1]]$name
[1] "name"
[[1]][[1]][[1]][[1]]$stage
[1] "stage"
[[1]][[1]]$date
[1] "date"
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[[2]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2
[[2]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"
[[2]][[1]][[1]][[1]]$name
[1] "name"
[[2]][[1]][[1]][[1]]$stage
[1] "stage"
[[2]][[1]]$date
[1] "date"
最终结果如下所示:
date name speech1 speech2
1
2
我想用我需要的变量创建列,并从我从中提取它们的列表中创建行。在上面的示例中,我需要从所有主列表中检索变量 speech1、speech2、名称和日期,并转换为更简单的数据框。我不太确定执行此操作的最快方法,并且在过去的几天里一直在用 lapply() 和 purrr 敲我的头。理想情况下,我想将列表视为列中具有扁平化变量的 rowID——但这也很棘手。任何帮助表示赞赏。
通过遍历每个列表,将其展平并获取值,可以使用 base R 快速实现:
# Your data
list1 = list(speech1 = 1, speech2 = 2)
list2 = list(list1, randomvariable="rando")
list3 = list(list2) #container
list4 = list(list3, name="name", stage="stage")
list5 = list(list4) #container
list6 = list(list5, date="date")
listmain1 = list(list6)
listmain2 = list(list6)
listmain3 = list(listmain1, listmain2)
# Loop over each list inside listmain3
flatten_list <- lapply(listmain3, function(x) {
# Flatten the list and extract the values that
# you're interested in
unlist(x)[c("date", "name", "speech1", "speech2")]
})
# bind each separate listo into a data frame
as.data.frame(do.call(rbind, flatten_list))
#> date name speech1 speech2
#> 1 date name 1 2
#> 2 date name 1 2
除非您想将行名称映射到每个列表中的某些值,否则行名称的顺序应与列表的数量相同。也就是说,如果您在 48 个嵌套列表上 运行,行名称将下降到 1:48
,因此无需使用 row.names
参数。