提问者:小点点

行绑定嵌套列表中存在的所有tibble


如何行绑定嵌套列表中存在的所有tibble。下面是四个级别的示例

组合所有嵌套列表元素以创建一个具有五列的tibble-

  • 第一级列表元素
  • 二级列表元素
  • 第三级列表元素
  • 第四级列表元素
  • p
library(dplyr)



tbl <- list()

a1 = runif(2)
a2 = runif(2)
a3 = runif(2)
a4 = runif(2)
a5 = runif(2)
a6 = runif(2)
a7 = runif(2)
a8 = runif(2)

tbl[["A"]][["m"]] [["a"]][["x"]] <- tibble(p = a1)
tbl[["A"]][["m"]] [["a"]][["y"]] <- tibble(p = a2)

tbl[["A"]][["m"]] [["b"]][["x"]] <- tibble(p = a3)
tbl[["A"]][["m"]] [["b"]][["y"]] <- tibble(p = a4)

tbl[["A"]][["n"]] [["a"]][["x"]] <- tibble(p = a5)
tbl[["A"]][["n"]] [["a"]][["y"]] <- tibble(p = a6)

tbl[["B"]][["n"]] [["b"]][["x"]] <- tibble(p = a7)
tbl[["A"]][["n"]] [["b"]][["y"]] <- tibble(p = a8)

tbl[["B"]][["m"]] [["a"]][["x"]] <- tibble(p = a1)
tbl[["B"]][["m"]] [["a"]][["y"]] <- tibble(p = a2)

tbl[["B"]][["m"]] [["b"]][["x"]] <- tibble(p = a3)
tbl[["B"]][["m"]] [["b"]][["y"]] <- tibble(p = a4)

tbl[["B"]][["n"]] [["a"]][["x"]] <- tibble(p = a5)
tbl[["B"]][["n"]] [["a"]][["y"]] <- tibble(p = a6)

tbl[["B"]][["n"]] [["b"]][["x"]] <- tibble(p = a7)
tbl[["B"]][["n"]] [["b"]][["y"]] <- tibble(p = a8)




# p

# expected results

tbl_expected <- tibble(first_element = c(rep("A", 16), rep("B", 16)),
                       
                       second_element = c(rep("m", 8), rep("n", 8), rep("m", 8), rep("n", 8)),
                       
                       third_element = c(rep("a", 4), rep("b", 4), rep("a", 4), rep("b", 4),
                                          rep("a", 4), rep("b", 4), rep("a", 4), rep("b", 4)),
                       
                       forth_element = c(rep("x", 2), rep("y", 2), rep("x", 2), rep("y", 2),
                                         rep("x", 2), rep("y", 2), rep("x", 2), rep("y", 2),
                                         rep("x", 2), rep("y", 2), rep("x", 2), rep("y", 2),
                                         rep("x", 2), rep("y", 2), rep("x", 2), rep("y", 2)),
                       
                     p = c(a1, a2, a3, a4, a5, a6, a7, a8, a1, a2, a3, a4, a5, a6, a7, a8))


共1个答案

匿名用户

我们可以将mapbind_rows一起使用,同时指定. id来创建嵌套列表的内部和外部名称

library(purrr)
library(dplyr)
map_dfr(tbl, ~ bind_rows(.x, .id = 'second_element'),
     .id = 'first_element')

-输出

# A tibble: 8 × 3
  first_element second_element      p
  <chr>         <chr>           <dbl>
1 a             x              0.307 
2 a             x              0.524 
3 a             y              0.827 
4 a             y              0.437 
5 b             x              0.236 
6 b             x              0.0944
7 b             y              0.590 
8 b             y              0.787 

对于具有更多嵌套的新数据集,我们可以使用递归函数rrapple

library(rrapply)
library(tidyr)
rrapply(tbl, how = 'melt') %>% 
   unnest(value) %>%  
   select(-L5) %>% 
   set_names(c('first_element', 'second_element',
      'third_element', 'fourth_element',  'p')) 

-输出

# A tibble: 30 × 5
   first_element second_element third_element fourth_element       p
   <chr>         <chr>          <chr>         <chr>            <dbl>
 1 A             m              a             x              0.409  
 2 A             m              a             x              0.782  
 3 A             m              a             y              0.832  
 4 A             m              a             y              0.769  
 5 A             m              b             x              0.365  
 6 A             m              b             x              0.401  
 7 A             m              b             y              0.00445
 8 A             m              b             y              0.667  
 9 A             n              a             x              0.00853
10 A             n              a             x              0.0553 
# … with 20 more rows