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 convert a list of tibbles tino a data.frame in R?

I have a list of tibbles, I would like to convert into a data.frame, whereas the name of the single tibbles can be taken as a column name

This is an example of list tibbles:

list(`2016-07-20` = structure(list(package = c("filehash", "weathermetrics"
), n = c(179L, 7L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L)), `2016-06-21` = structure(list(package = c("filehash", 
"weathermetrics"), n = c(159L, 6L)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -2L)))

which looks like that:

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

$`2016-07-20`
# A tibble: 2 × 2
  package            n
  <chr>          <int>
1 filehash         179
2 weathermetrics     7

$`2016-06-21`
# A tibble: 2 × 2
  package            n
  <chr>          <int>
1 filehash         159
2 weathermetrics     6

and I would like it to be converted in such a data.frame:

  package    2016-07-20    2016-06-21 
1 filehash          179           159
2 weathermetrics      7             6

any ideas how can this be done?

i have already tried do.call, unlist or t(sapply), but none helped me reached the desired goal.

thanks

Assa

>Solution :

You can do:

library(dplyr)
library(tidyr)

l %>% 
  bind_rows(.id = "date") %>% 
  pivot_wider(names_from = "date", values_from = "n")

output

  package        `2016-07-20` `2016-06-21`
1 filehash                179          159
2 weathermetrics            7            6
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