Home > Enterprise >  How to rbind data.frames contained in sublists in R
How to rbind data.frames contained in sublists in R

Time:08-22

mylist <- list(list(structure(list(beta = c(48.8486939314123, -0.791098772521124, 
-17.1655206378182, -5.71137384209288, 5.48759276652723, 32.9922867794022, 
2.678027817518, 7.07190050739792, 1.2366953771653, 3.34913038674097
), name = c("V3", "V4", "V5", "V6", "V7", "V3", "V4", "V5", "V6", 
"V7"), variant = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), site = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c("V3", "V4", 
"V5", "V6", "V7", "V31", "V41", "V51", "V61", "V71"), class = "data.frame"), 
    structure(list(gamma = c(-3.3478378772043, -4.08286261159924
    ), variant = 1:2, site = c(1L, 1L)), row.names = c(NA, -2L
    ), class = "data.frame"), structure(list(alpha = c(0.676035079065653, 
    1.05834914172426), variant = 1:2, site = c(1L, 1L)), row.names = c(NA, 
    -2L), class = "data.frame")), list(structure(list(beta = c(32.9922867794022, 
2.678027817518, 7.07190050739792, 1.2366953771653, 3.34913038674097, 
48.8486939314123, -0.791098772521124, -17.1655206378182, -5.71137384209288, 
5.48759276652723), name = c("V3", "V4", "V5", "V6", "V7", "V3", 
"V4", "V5", "V6", "V7"), variant = c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L), site = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L)), row.names = c("V3", "V4", "V5", "V6", "V7", "V31", "V41", 
"V51", "V61", "V71"), class = "data.frame"), structure(list(gamma = c(-4.08286261159924, 
-3.3478378772043), variant = 1:2, site = c(2L, 2L)), row.names = c(NA, 
-2L), class = "data.frame"), structure(list(alpha = c(1.05834914172426, 
0.676035079065653), variant = 1:2, site = c(2L, 2L)), row.names = c(NA, 
-2L), class = "data.frame")))


> mylist
[[1]]
[[1]][[1]]
           beta name variant site
V3   48.8486939   V3       1    1
V4   -0.7910988   V4       1    1
V5  -17.1655206   V5       1    1
V6   -5.7113738   V6       1    1
V7    5.4875928   V7       1    1
V31  32.9922868   V3       2    1
V41   2.6780278   V4       2    1
V51   7.0719005   V5       2    1
V61   1.2366954   V6       2    1
V71   3.3491304   V7       2    1

[[1]][[2]]
      gamma variant site
1 -3.347838       1    1
2 -4.082863       2    1

[[1]][[3]]
      alpha variant site
1 0.6760351       1    1
2 1.0583491       2    1


[[2]]
[[2]][[1]]
           beta name variant site
V3   32.9922868   V3       1    2
V4    2.6780278   V4       1    2
V5    7.0719005   V5       1    2
V6    1.2366954   V6       1    2
V7    3.3491304   V7       1    2
V31  48.8486939   V3       2    2
V41  -0.7910988   V4       2    2
V51 -17.1655206   V5       2    2
V61  -5.7113738   V6       2    2
V71   5.4875928   V7       2    2

[[2]][[2]]
      gamma variant site
1 -4.082863       1    2
2 -3.347838       2    2

[[2]][[3]]
      alpha variant site
1 1.0583491       1    2
2 0.6760351       2    2

In each sublist, there are 3 data.frames, I would like to rbind these so that the final output looks like this:

new_mylist

[[1]]
            beta name variant site
V3    48.8486939   V3       1    1
V4    -0.7910988   V4       1    1
V5   -17.1655206   V5       1    1
V6    -5.7113738   V6       1    1
V7     5.4875928   V7       1    1
V31   32.9922868   V3       2    1
V41    2.6780278   V4       2    1
V51    7.0719005   V5       2    1
V61    1.2366954   V6       2    1
V71    3.3491304   V7       2    1
V32   32.9922868   V3       1    2
V42    2.6780278   V4       1    2
V52    7.0719005   V5       1    2
V62    1.2366954   V6       1    2
V72    3.3491304   V7       1    2
V311  48.8486939   V3       2    2
V411  -0.7910988   V4       2    2
V511 -17.1655206   V5       2    2
V611  -5.7113738   V6       2    2
V711   5.4875928   V7       2    2

[[2]]
      gamma variant site
1 -3.347838       1    1
2 -4.082863       2    1
3 -4.082863       1    2
4 -3.347838       2    2

[[3]]
      alpha variant site
1 0.6760351       1    1
2 1.0583491       2    1
3 1.0583491       1    2
4 0.6760351       2    2

CodePudding user response:

We could transpose and bind

library(purrr)
library(dplyr)
transpose(mylist) %>%
     map(bind_rows)

CodePudding user response:

in Base R you could do:

do.call(Map, c(rbind, mylist))

[[1]]
            beta name variant site
V3    48.8486939   V3       1    1
V4    -0.7910988   V4       1    1
V5   -17.1655206   V5       1    1
V6    -5.7113738   V6       1    1
V7     5.4875928   V7       1    1
V31   32.9922868   V3       2    1
V41    2.6780278   V4       2    1
V51    7.0719005   V5       2    1
V61    1.2366954   V6       2    1
V71    3.3491304   V7       2    1
V32   32.9922868   V3       1    2
V42    2.6780278   V4       1    2
V52    7.0719005   V5       1    2
V62    1.2366954   V6       1    2
V72    3.3491304   V7       1    2
V311  48.8486939   V3       2    2
V411  -0.7910988   V4       2    2
V511 -17.1655206   V5       2    2
V611  -5.7113738   V6       2    2
V711   5.4875928   V7       2    2

[[2]]
      gamma variant site
1 -3.347838       1    1
2 -4.082863       2    1
3 -4.082863       1    2
4 -3.347838       2    2

[[3]]
      alpha variant site
1 0.6760351       1    1
2 1.0583491       2    1
3 1.0583491       1    2
4 0.6760351       2    2

CodePudding user response:

> lapply(1:length(mylist[[1]]),\(i)do.call(rbind,lapply(mylist,`[[`,i)))
[[1]]
            beta name variant site
V3    48.8486939   V3       1    1
V4    -0.7910988   V4       1    1
V5   -17.1655206   V5       1    1
V6    -5.7113738   V6       1    1
V7     5.4875928   V7       1    1
V31   32.9922868   V3       2    1
V41    2.6780278   V4       2    1
V51    7.0719005   V5       2    1
V61    1.2366954   V6       2    1
V71    3.3491304   V7       2    1
V32   32.9922868   V3       1    2
V42    2.6780278   V4       1    2
V52    7.0719005   V5       1    2
V62    1.2366954   V6       1    2
V72    3.3491304   V7       1    2
V311  48.8486939   V3       2    2
V411  -0.7910988   V4       2    2
V511 -17.1655206   V5       2    2
V611  -5.7113738   V6       2    2
V711   5.4875928   V7       2    2

[[2]]
      gamma variant site
1 -3.347838       1    1
2 -4.082863       2    1
3 -4.082863       1    2
4 -3.347838       2    2

[[3]]
      alpha variant site
1 0.6760351       1    1
2 1.0583491       2    1
3 1.0583491       1    2
4 0.6760351       2    2
  • Related