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 save each named tibble in a list, as a separate tibble or dataframe in one run

This question is related to this How to loop over the columns in a dataframe, apply spread, and create a new dataframe in R?

I have a list with 3 tibbles:

$Peter
# A tibble: 3 x 4
  date       Peter   Ben  Mary
  <chr>      <dbl> <dbl> <dbl>
1 2020-03-30   0.4  NA    NA  
2 2020-10-14  NA     0.6  NA  
3 2020-12-06  NA    NA     0.7

$Ben
# A tibble: 3 x 4
  date       Peter   Ben  Mary
  <chr>      <dbl> <dbl> <dbl>
1 2020-03-30   0.5  NA    NA  
2 2020-10-14  NA     0.4  NA  
3 2020-12-06  NA    NA     0.2

$Mary
# A tibble: 3 x 4
  date       Peter   Ben  Mary
  <chr>      <dbl> <dbl> <dbl>
1 2020-03-30   0.2  NA    NA  
2 2020-10-14  NA     0.1  NA  
3 2020-12-06  NA    NA     0.9

mylistdf <- list(Peter = structure(list(date = c("2020-03-30", "2020-10-14", 
"2020-12-06"), Peter = c(0.4, NA, NA), Ben = c(NA, 0.6, NA), 
    Mary = c(NA, NA, 0.7)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")), Ben = structure(list(date = c("2020-03-30", 
"2020-10-14", "2020-12-06"), Peter = c(0.5, NA, NA), Ben = c(NA, 
0.4, NA), Mary = c(NA, NA, 0.2)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")), Mary = structure(list(date = c("2020-03-30", 
"2020-10-14", "2020-12-06"), Peter = c(0.2, NA, NA), Ben = c(NA, 
0.1, NA), Mary = c(NA, NA, 0.9)), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame")))

How can I save all this 3 tibbles as an own tibble in the global environment:

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

Expected output would be kind of having Peter, Ben, Mary as tibbles or dataframes in the global environment.

I know we could do:

Peter <- mylistdf$Peter
Ben <- mylistdf$Ben
Mary <- mylistdf$Mary

But I whish to do it in one run with map or similar like:

mylistdf %>%
  .....

>Solution :

We could use %=%

library(collapse)
names(mylistdf) %=% mylistdf

-checking

> Peter
# A tibble: 3 × 4
  date       Peter   Ben  Mary
  <chr>      <dbl> <dbl> <dbl>
1 2020-03-30   0.4  NA    NA  
2 2020-10-14  NA     0.6  NA  
3 2020-12-06  NA    NA     0.7
> Mary
# A tibble: 3 × 4
  date       Peter   Ben  Mary
  <chr>      <dbl> <dbl> <dbl>
1 2020-03-30   0.2  NA    NA  
2 2020-10-14  NA     0.1  NA  
3 2020-12-06  NA    NA     0.9
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