R中的一个数据帧中有多个列表

时间:2017-10-26 14:40:35

标签: r list dataframe

所以我正在运行一个包,其中我正在使用的函数的输出与此类似:

        area     ID   structure
1       150       1     house

我通过循环浏览一些东西得到了其中的几个。基本上这是我的循环函数:

for (k in 1:length(models)) {
    for (l in 1:length(patients)) {
        print(result[[l]][[k]])
        tableData[[l]][[k]] <- do.call(rbind, result[[l]][[k]])
    }
}

所以print(result[[l]][[k]])给出了我在开始时给你看的输出。所以我的问题是将所有这些都放在一个数据帧中。到目前为止它只是不起作用,即do.call函数,我读过的是将列表组合到数据帧时使用的函数。

那我在哪里错了?

更新:

dput()输出(在这种情况下,area = value):

list(list(structure(list(value = 0.0394797760472196, ID = "1 house", 
    structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
"ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
    structure(list(value = 0.0394797760472196, ID = "1 house", 
        structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame")), 
    list(structure(list(value = 0.0306923865158472, ID = "2 house", 
        structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
        structure(list(value = 0.0306923865158472, ID = "2 house", 
            structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
        "ID", "structure", "model"), row.names = c(NA, -1L
        ), class = "data.frame")))
list(list(structure(list(value = 0.0394797760472196, ID = "1 house", 
    structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
"ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
    structure(list(value = 0.0394797760472196, ID = "1 house", 
        structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame")), 
    list(structure(list(value = 0.0306923865158472, ID = "2 house", 
        structure = "house", model = structure(1L, .Label = "wood", class = "factor")), .Names = c("value", 
    "ID", "structure", "model"), row.names = c(NA, -1L), class = "data.frame"), 
        structure(list(value = 0.0306923865158472, ID = "2 house", 
            structure = "house", model = structure(1L, .Label = "stone", class = "factor")), .Names = c("value", 
        "ID", "structure", "model"), row.names = c(NA, -1L
        ), class = "data.frame")))

1 个答案:

答案 0 :(得分:2)

修改:我最初使用purrr::map_dfr来解决此问题,但purrr::reduce更合适。

列表嵌套意味着我们必须将行绑定两次。以下是使用purrrdplyr个包并将dput列表分配给变量my_list的解决方案:


library(purrr)
library(dplyr)

my_df <- reduce(my_list, bind_rows)
#> Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector

#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector
#> Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector

#> Warning in bind_rows_(x, .id): binding character and factor vector,
#> coercing into character vector
my_df
#>        value      ID structure model
#> 1 0.03947978 1 house     house  wood
#> 2 0.03947978 1 house     house stone
#> 3 0.03069239 2 house     house  wood
#> 4 0.03069239 2 house     house stone

我发现使用purrr方式的地图比do.call更直观。如果这有帮助,请告诉我!