从嵌套列表中提取数据并返回data.frame

时间:2017-09-07 03:56:26

标签: r dataframe lapply sapply do.call

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,来自my previous post,我刚刚了解到您可以使用do.call函数对其进行格式化,我想将其作为数据框输出。

以下是该帖子中answer的代码:

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

data.frame中的错误(名称(temp)==&#34;名称&#34;,temp [名称(临时)==&#34; department.name&#34;],:
    参数意味着不同的行数: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"))

1 个答案:

答案 0 :(得分:4)

基于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")]

将每个嵌套列表转换为数据框&amp;将它们绑在一起:

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