我有嵌套的tibble,在一个列表中,在一个列表中, struct 如下:

我基本上需要重新绑定所有内容中的数据帧.结果将是一个列表中的6个数据帧,每个季节(冬季/夏季)和物种组合一个.

> glimpse(list_graphs)

List of 2
 $ winter:List of 3
  ..$ species1: tibble [161 x 1] (S3: tbl_df/tbl/data.frame)
  ..$ species2: tibble [38 x 1] (S3: tbl_df/tbl/data.frame)
  ..$ species3: tibble [72 x 1] (S3: tbl_df/tbl/data.frame)
 $ summer:List of 3
  ..$ species1: tibble [33 x 1] (S3: tbl_df/tbl/data.frame)
  ..$ species2: tibble [2 x 1] (S3: tbl_df/tbl/data.frame)
  ..$ species3: tibble [52 x 1] (S3: tbl_df/tbl/data.frame)

我试过了:

map(list_graphs, function(x) {
  map(x, function(x){
    map(x,~do.call(rbind,x))
    }
  )
})

但是,这会导致以下不正确的结果:

List of 2
 $ winter:List of 3
  ..$ species1:List of 1
  .. ..$ graph_layer:List of 161
  .. .. ..- attr(*, "dim")= int [1:2] 1 161
  .. .. ..- attr(*, "dimnames")=List of 2
  ..$ species2:List of 1
  .. ..$ graph_layer:List of 38
  .. .. ..- attr(*, "dim")= int [1:2] 1 38
  .. .. ..- attr(*, "dimnames")=List of 2
  ..$ species3:List of 1
  .. ..$ graph_layer:List of 72
  .. .. ..- attr(*, "dim")= int [1:2] 1 72
  .. .. ..- attr(*, "dimnames")=List of 2
 $ summer:List of 3
  ..$ species1:List of 1
  .. ..$ graph_layer:List of 33
  .. .. ..- attr(*, "dim")= int [1:2] 1 33
  .. .. ..- attr(*, "dimnames")=List of 2
  ..$ species2:List of 1
  .. ..$ graph_layer:List of 2
  .. .. ..- attr(*, "dim")= int [1:2] 1 2
  .. .. ..- attr(*, "dimnames")=List of 2
  ..$ species3:List of 1
  .. ..$ graph_layer:List of 52
  .. .. ..- attr(*, "dim")= int [1:2] 1 52
  .. .. ..- attr(*, "dimnames")=List of 2

我肯定这是一个相对容易的解决办法,但我就是想不出来.我希望有人可以建议解决方案的基础上,我以上的 struct ,如果不是,我会try 和得到一个工作的正则表达式数据帧.

谢谢

推荐答案

由于您没有提供可复制的示例,下面是我理解您正在处理的内容的方式:

You have (something like)

l <- list(
      winter = list(
        species1 = list(tibble(x = 1), tibble(x = 2), tibble(x = 3)), 
        species2 = list(tibble(x = 4), tibble(x = 5), tibble(x = 6)), 
        species3 = list(tibble(x = 7), tibble(x = 8), tibble(x = 9)) 
        ),
     summer = list(
       species1 = list(tibble(x = 10), tibble(x = 11), tibble(x = 12)), 
       species2 = list(tibble(x = 13), tibble(x = 14), tibble(x = 15)), 
       species3 = list(tibble(x = 16), tibble(x = 17), tibble(x = 18)) 
     )
    )

下面是一种保留名称并返回(与行绑定的)tibble列表的方法:

map(names(l), function(name) set_names( 
                              map(l[[name]], ~ map_dfr(.x, ~ .x)), 
                              paste0, name
                             )
  ) %>% 
   flatten()

Outcome (of 100 for brevity)

List of 6
 $ species1winter: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 1 2 3
 $ species2winter: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 4 5 6
 $ species3winter: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 7 8 9
 $ species1summer: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 10 11 12
 $ species2summer: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 13 14 15
 $ species3summer: tibble [3 × 1] (S3: tbl_df/tbl/data.frame)
  ..$ x: num [1:3] 16 17 18

R相关问答推荐

在ggplot的注释表格中突出显示最大值

R的GG平行坐标图中的排序变量

ggplot geom_smooth()用于线性回归虚拟变量-没有回归线

任意列的欧几里得距离

如何将旋转后的NetCDF转换回正常的纬度/经度网格,并使用R?

使用外部文件分配变量名及其值

R中的时间序列(Ts)函数计数不正确

在不丢失空值的情况下取消列出嵌套列表

如何在R库GoogleDrive中完全删除预先授权的Google帐户?

在R函数中使用加号

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

正在导出默认的RStudio主题,还是设置括号 colored颜色 ?

如何将一个方阵分解成没有循环的立方体

如何移动点以使它们的打印不重叠

如何将EC50值绘制在R中的剂量-react 曲线上?

排序R矩阵的行和列

具有自定义仓位限制和计数的GGPLATE直方图

识别部分重复行,其中一行为NA,其重复行为非NA

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?