How to row bind all tibbles present in the nested list. Below is an example of four levels
combine all nested list elements to create a single tibble which has five columns -
- first level list elements
- second level list elements
- third level list elements
- fourth level list elements
- 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))