Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine lists that include tibbles?

Here are lists that include tibbels:

test=list()
l <- list(list(var = "bb",b =  2, c = 3, d = 5), list(var = "a", b = 3, c  = 2,  d = 4))

l2 <- map(l,~data.frame(.))
l3 <-map_dfr(l2,~mutate_all(.,as.character))  
l4<-as_tibble(l3)
test[[1]]=l4
names(test)[1]="par"
test[[2]]=l4
names(test)[2]="mas"

myli=list()
myli[[1]]=l4
names(myli)[1]="par"
myli[[2]]=l4
names(myli)[2]="mas"

I would like to extract the name, var, and d and combine:

The output that I need would be like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  var        value    list     name 
  bb         5         test    par
  a          4         test    par
  bb         5         test    mas 
  a          4         test    mas
  bb         5         myli    par
  a          4         myli    par
  bb         5         myli    mas 
  a          4         myli    mas

>Solution :

Using dplyr::bind_rows you could do:

library(purrr)
library(dplyr)

map(list(test = test, myli = myli), bind_rows, .id = "name") %>% 
  bind_rows(.id = "list") %>% 
  select(var, value = d, list, name)
#> # A tibble: 8 × 4
#>   var   value list  name 
#>   <chr> <chr> <chr> <chr>
#> 1 bb    5     test  par  
#> 2 a     4     test  par  
#> 3 bb    5     test  mas  
#> 4 a     4     test  mas  
#> 5 bb    5     myli  par  
#> 6 a     4     myli  par  
#> 7 bb    5     myli  mas  
#> 8 a     4     myli  mas
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading