从嵌套列表和 return 中提取数据 data.frame
Extract data from a nested list and return a data.frame
dput(head(z2,10))
structure(list(name = list("Mary"), department = structure(list(
name = list("English")), .Names = "name", id = "300"), department = structure(list(
name = list("Math")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("German")), .Names = "name", id = "302"),
department = structure(list(name = list("German")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "303"),
department = structure(list(name = list("Sport")), .Names = "name", id = "305"),
department = structure(list(name = list("Chemistry")), .Names = "name", id = "306"),
department = structure(list(name = list("Science")), .Names = "name", id = "308")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department"))
我正在尝试从列表中提取信息并将其放入 data.frame,来自 ,我刚刚了解到您可以使用 do.call
函数来格式化它,我想将它输出为数据框。
这是 中的代码 post:
c <- do.call(rbind,
lapply(unname(z2),
function(x) {
temp <- unlist(x)
data.frame(names(temp) == "name",
temp[names(temp) == "department.name"],
unlist(sapply(x, attr, "id")),
row.names=NULL)
}))
Error in data.frame(names(temp) == "name", temp[names(temp) == "department.name"], :
arguments imply differing number of rows: 1, 0
新数据:
structure(list(code = list("1"), note = list("success"), category = structure(list(
name = list("Mary"), department = structure(list(name = list(
"Math")), .Names = "name", id = "300"), department = structure(list(
name = list("English")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "317")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department")), category = structure(list(
name = list("Kevin"), department = structure(list(name = list(
"Physics")), .Names = "name", id = "12G0"), department = structure(list(
name = list("German")), .Names = "name", id = "321"),
department = structure(list(name = list("French")), .Names = "name", id = "325"),
department = structure(list(name = list("Spanish")), .Names = "name", id = "427")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department")), category = structure(list(
name = list("Andy"), department = structure(list(name = list(
"Swedish")), .Names = "name", id = "330"), department = structure(list(
name = list("Danish")), .Names = "name", id = "331"),
department = structure(list(name = list("Russian")), .Names = "name", id = "332"),
department = structure(list(name = list("Japanese")), .Names = "name", id = "341")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department")), category = structure(list(
name = list("Nana"), department = structure(list(name = list(
"Arabic")), .Names = "name", id = "200"), department = structure(list(
name = list("African")), .Names = "name", id = "201"),
department = structure(list(name = list("Sport")), .Names = "name", id = "202"),
department = structure(list(name = list("Korean")), .Names = "name", id = "211")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department")), category = structure(list(name = list("Sandy"),
department = structure(list(name = list("Vocals")), .Names = "name", id = "100"),
department = structure(list(name = list("Language")), .Names = "name", id = "515")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department"
))), .Names = c("code", "note", "category", "category", "category",
"category", "category"))
根据 OP 的带有嵌套列表的新样本编辑的答案,每个代表一个用户(修改 版本的数据集复制如下,因为每个类别中的名称多于元素,这没有真的很有道理)。
数据:
z2 <- structure(list(code = list("1"),
note = list("success"),
category = structure(list(name = list("Mary"),
department = structure(list(name = list("Math")), .Names = "name", id = "300"),
department = structure(list(name = list("English")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "317")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Kevin"),
department = structure(list(name = list("Physics")), .Names = "name", id = "12G0"),
department = structure(list(name = list("German")), .Names = "name", id = "321"),
department = structure(list(name = list("French")), .Names = "name", id = "325"),
department = structure(list(name = list("Spanish")), .Names = "name", id = "427")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Andy"),
department = structure(list(name = list("Swedish")), .Names = "name", id = "330"),
department = structure(list(name = list("Danish")), .Names = "name", id = "331"),
department = structure(list(name = list("Russian")), .Names = "name", id = "332"),
department = structure(list(name = list("Japanese")), .Names = "name", id = "341")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Nana"),
department = structure(list(name = list("Arabic")), .Names = "name", id = "200"),
department = structure(list(name = list("African")), .Names = "name", id = "201"),
department = structure(list(name = list("Sport")), .Names = "name", id = "202"),
department = structure(list(name = list("Korean")), .Names = "name", id = "211")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Sandy"),
department = structure(list(name = list("Vocals")), .Names = "name", id = "100"),
department = structure(list(name = list("Language")), .Names = "name", id = "515")),
.Names = c("name", "department", "department"))),
.Names = c("code", "note", "category", "category", "category", "category", "category"))
从列表中删除不需要的元素:
# keep only category elements (i.e. drop code, note, etc. from the list)
z2 <- z2[which(names(z2)=="category")]
将每个嵌套列表转换为数据框并将它们绑定在一起:
do.call(rbind,
lapply(unname(z2),
function(y){
data.frame(
name = y[[which(names(y)=="name")]][[1]],
department = sapply(y[which(names(y)=="department")], function(x){x[[1]][[1]]}),
id = sapply(y[which(names(y)=="department")], function(x){attr(x, "id")})
)
}))
name department id
1 Mary Math 300
2 Mary English 301
3 Mary Chinese 302f
4 Mary Music 317
5 Kevin Physics 12G0
6 Kevin German 321
7 Kevin French 325
8 Kevin Spanish 427
9 Andy Swedish 330
10 Andy Danish 331
11 Andy Russian 332
12 Andy Japanese 341
13 Nana Arabic 200
14 Nana African 201
15 Nana Sport 202
16 Nana Korean 211
17 Sandy Vocals 100
18 Sandy Language 515
dput(head(z2,10))
structure(list(name = list("Mary"), department = structure(list(
name = list("English")), .Names = "name", id = "300"), department = structure(list(
name = list("Math")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("German")), .Names = "name", id = "302"),
department = structure(list(name = list("German")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "303"),
department = structure(list(name = list("Sport")), .Names = "name", id = "305"),
department = structure(list(name = list("Chemistry")), .Names = "name", id = "306"),
department = structure(list(name = list("Science")), .Names = "name", id = "308")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department"))
我正在尝试从列表中提取信息并将其放入 data.frame,来自 do.call
函数来格式化它,我想将它输出为数据框。
这是
c <- do.call(rbind,
lapply(unname(z2),
function(x) {
temp <- unlist(x)
data.frame(names(temp) == "name",
temp[names(temp) == "department.name"],
unlist(sapply(x, attr, "id")),
row.names=NULL)
}))
Error in data.frame(names(temp) == "name", temp[names(temp) == "department.name"], :
arguments imply differing number of rows: 1, 0
新数据:
structure(list(code = list("1"), note = list("success"), category = structure(list(
name = list("Mary"), department = structure(list(name = list(
"Math")), .Names = "name", id = "300"), department = structure(list(
name = list("English")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "317")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department")), category = structure(list(
name = list("Kevin"), department = structure(list(name = list(
"Physics")), .Names = "name", id = "12G0"), department = structure(list(
name = list("German")), .Names = "name", id = "321"),
department = structure(list(name = list("French")), .Names = "name", id = "325"),
department = structure(list(name = list("Spanish")), .Names = "name", id = "427")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department")), category = structure(list(
name = list("Andy"), department = structure(list(name = list(
"Swedish")), .Names = "name", id = "330"), department = structure(list(
name = list("Danish")), .Names = "name", id = "331"),
department = structure(list(name = list("Russian")), .Names = "name", id = "332"),
department = structure(list(name = list("Japanese")), .Names = "name", id = "341")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department",
"department", "department", "department")), category = structure(list(
name = list("Nana"), department = structure(list(name = list(
"Arabic")), .Names = "name", id = "200"), department = structure(list(
name = list("African")), .Names = "name", id = "201"),
department = structure(list(name = list("Sport")), .Names = "name", id = "202"),
department = structure(list(name = list("Korean")), .Names = "name", id = "211")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department")), category = structure(list(name = list("Sandy"),
department = structure(list(name = list("Vocals")), .Names = "name", id = "100"),
department = structure(list(name = list("Language")), .Names = "name", id = "515")), .Names = c("name",
"department", "department", "department", "department", "department", "department",
"department", "department", "department", "department", "department", "department"
))), .Names = c("code", "note", "category", "category", "category",
"category", "category"))
根据 OP 的带有嵌套列表的新样本编辑的答案,每个代表一个用户(修改 版本的数据集复制如下,因为每个类别中的名称多于元素,这没有真的很有道理)。
数据:
z2 <- structure(list(code = list("1"),
note = list("success"),
category = structure(list(name = list("Mary"),
department = structure(list(name = list("Math")), .Names = "name", id = "300"),
department = structure(list(name = list("English")), .Names = "name", id = "301"),
department = structure(list(name = list("Chinese")), .Names = "name", id = "302f"),
department = structure(list(name = list("Music")), .Names = "name", id = "317")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Kevin"),
department = structure(list(name = list("Physics")), .Names = "name", id = "12G0"),
department = structure(list(name = list("German")), .Names = "name", id = "321"),
department = structure(list(name = list("French")), .Names = "name", id = "325"),
department = structure(list(name = list("Spanish")), .Names = "name", id = "427")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Andy"),
department = structure(list(name = list("Swedish")), .Names = "name", id = "330"),
department = structure(list(name = list("Danish")), .Names = "name", id = "331"),
department = structure(list(name = list("Russian")), .Names = "name", id = "332"),
department = structure(list(name = list("Japanese")), .Names = "name", id = "341")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Nana"),
department = structure(list(name = list("Arabic")), .Names = "name", id = "200"),
department = structure(list(name = list("African")), .Names = "name", id = "201"),
department = structure(list(name = list("Sport")), .Names = "name", id = "202"),
department = structure(list(name = list("Korean")), .Names = "name", id = "211")),
.Names = c("name", "department", "department", "department", "department")),
category = structure(list(name = list("Sandy"),
department = structure(list(name = list("Vocals")), .Names = "name", id = "100"),
department = structure(list(name = list("Language")), .Names = "name", id = "515")),
.Names = c("name", "department", "department"))),
.Names = c("code", "note", "category", "category", "category", "category", "category"))
从列表中删除不需要的元素:
# keep only category elements (i.e. drop code, note, etc. from the list)
z2 <- z2[which(names(z2)=="category")]
将每个嵌套列表转换为数据框并将它们绑定在一起:
do.call(rbind,
lapply(unname(z2),
function(y){
data.frame(
name = y[[which(names(y)=="name")]][[1]],
department = sapply(y[which(names(y)=="department")], function(x){x[[1]][[1]]}),
id = sapply(y[which(names(y)=="department")], function(x){attr(x, "id")})
)
}))
name department id
1 Mary Math 300
2 Mary English 301
3 Mary Chinese 302f
4 Mary Music 317
5 Kevin Physics 12G0
6 Kevin German 321
7 Kevin French 325
8 Kevin Spanish 427
9 Andy Swedish 330
10 Andy Danish 331
11 Andy Russian 332
12 Andy Japanese 341
13 Nana Arabic 200
14 Nana African 201
15 Nana Sport 202
16 Nana Korean 211
17 Sandy Vocals 100
18 Sandy Language 515