我有一个三层嵌套的列表,我想将其转换为数据框。这里有一个类似的问题,但是我的区别是我的数据具有不相等的行号。我不知道如何用不相等的行来表示这种复杂的数据,所以我提供了数据结构的图片。
我尝试了下面的代码,但是将错误粘贴到了下面。
dfr <- do.call(rbind,lapply(a_local,function(x) as.data.frame.list(lapply(x,as.data.frame.list))))
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
bind_rows(a_local)
Error: Argument 1 can't be a list containing data frames
编辑:下面是类似输出的代表
list(list(`2000` = structure(list(measure = c("raw alpha", "raw alpha",
"raw alpha"), q = 0:2, type_level = c("types", "types", "types"
), type_name = c("", "", ""), partition_level = c("metacommunity",
"metacommunity", "metacommunity"), partition_name = c("", "",
""), diversity = c(130, 47.6362106646841, 29.1304206610839)), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame")), `2001` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(128,
46.3663699146013, 29.0085607409981)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2002` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(112,
34.8429465109795, 19.6418918995337)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2003` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(138,
36.278539988337, 20.2563354116874)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2004` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(152,
41.5585943976923, 24.4257576835118)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2005` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(136,
53.0647037177875, 37.9006652475377)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2006` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(142,
48.4695259044628, 30.8725886657974)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2007` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(161,
52.4982892170671, 33.4245776914158)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2008` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(190,
37.6842601161025, 17.1959659030679)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2009` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(177,
50.6980919345464, 30.4026099015626)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2010` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(165,
33.6759915646318, 16.5874806230282)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2011` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(191,
37.967808614851, 16.1793729566448)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))), list(`2000` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(62,
25.1734201802724, 16.2717853623945)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2001` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(54,
18.6719253089667, 11.2581657586825)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2002` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(56,
19.0231843901984, 11.0947252228356)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2003` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(57,
22.8282386416829, 12.5165034323462)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2004` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(51,
30.8105815978442, 22.1295834457466)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2005` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(50,
33.9034098784768, 26.4879053020106)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2006` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(62,
33.7190440665969, 22.2146972715582)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2007` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(72,
39.0907734308199, 21.7383249141099)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2008` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(79,
41.1438272519128, 27.8199767081214)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2009` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(72,
38.7526553238673, 23.7441284758299)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2010` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(66,
32.6248446686476, 19.6259433905257)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2011` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(68,
41.3940021837551, 30.5861687624241)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))), list(`2002` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(38,
10.278518823847, 5.95517798951935)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2003` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(38,
12.9546999522406, 7.32849312366838)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2006` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(37,
17.7209870045672, 12.4870099104185)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2007` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(40,
17.1610003189147, 10.3260620890448)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2008` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(53,
26.3148331651736, 17.1659824860019)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2009` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(47,
11.9312680860448, 5.41945336689468)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2011` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(45,
6.01237181210209, 2.42710546694491)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))), list(`2000` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(47,
16.0537839386773, 9.15401148634299)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2001` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(39,
14.0891052780818, 7.4340030245741)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2002` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(40,
19.9182625826876, 11.8134371553591)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2003` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(50,
22.5388076466994, 12.0191726459901)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2004` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(43,
24.2223797494203, 14.892217206058)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2006` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(36,
22.8858413683323, 14.3488216613739)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2007` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(57,
9.14200541985277, 3.11306568929166)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2008` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(48,
7.63659000253928, 2.66521736760175)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2009` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(48,
8.3410364450592, 3.27251065971041)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2010` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(58,
13.9406660758292, 4.74839528806686)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2011` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(54,
11.1318665817255, 3.58610226121249)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))), list(`2000` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(33,
16.6294253666135, 11.176343410935)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2001` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(30,
16.4347026429985, 12.077846009268)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2002` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(34,
17.3684052590679, 12.6615910826356)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2003` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(39,
17.2957608108924, 10.6444628801781)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2004` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(29,
16.1566079338594, 11.9277978772552)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2006` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(37,
18.894306584847, 12.3929163117495)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2007` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(47,
23.4879074697119, 15.4293690573247)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2008` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(46,
21.1396265765405, 12.6772679784588)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2010` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(46,
24.5337325449747, 16.7574484292759)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame")), `2011` = structure(list(
measure = c("raw alpha", "raw alpha", "raw alpha"), q = 0:2,
type_level = c("types", "types", "types"), type_name = c("",
"", ""), partition_level = c("metacommunity", "metacommunity",
"metacommunity"), partition_name = c("", "", ""), diversity = c(58,
25.500304964892, 14.2651685427877)), row.names = c(NA, -3L
), class = c("tbl_df", "tbl", "data.frame"))))
所需的数据帧输出将类似于
ID (as list#) measure q diversity Year
1 alpha 0 0.97279 2000
1 alpha 1 0.87381 2000
1 alpha 2 0.10823 2000
1 beta 0 0.55075 2000
1 beta 1 0.01355 2000
1 beta 2 0.10672 2000
1 gamma 0 0.55075 2000
1 gamma 1 0.01355 2000
1 gamma 2 0.10672 2000
1 alpha 0 0.1136 2001
,然后到第二年,直到下一个第一个分组列表出现。这有道理吗?
答案 0 :(得分:0)
我不知道为什么将嵌套列表的dput输出粘贴到R中时不会矢量化到对象中。但是我不是想解决这个问题!
我想出了将其转换为所需格式的正确方法!
out <- lapply(nested_list, function(x)
do.call("rbind", x))
bind_rows(out, .id = "column_label")
# A tibble: 156 x 8
column_label measure q type_level type_name partition_level partition_name diversity
<chr> <chr> <int> <chr> <chr> <chr> <chr> <dbl>
1 1 raw alpha 0 types "" metacommunity "" 130
2 1 raw alpha 1 types "" metacommunity "" 47.6
3 1 raw alpha 2 types "" metacommunity "" 29.1
4 1 raw alpha 0 types "" metacommunity "" 128
5 1 raw alpha 1 types "" metacommunity "" 46.4
6 1 raw alpha 2 types "" metacommunity "" 29.0
7 1 raw alpha 0 types "" metacommunity "" 112
8 1 raw alpha 1 types "" metacommunity "" 34.8
9 1 raw alpha 2 types "" metacommunity "" 19.6
10 1 raw alpha 0 types "" metacommunity "" 138