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

Combine data frames using full join and just ignore any dfs that are null

Example:

mydfs <- list(
  d1 = NULL,
  d2 = mtcars %>% rownames_to_column %>% head,
  d3 = mtcars %>% rownames_to_column %>% tail
)

# fail
mydfs$combined <- mydfs$d1 %>% 
  full_join(mydfs$d2, by = c('rowname'))

# works
mydfs$combined <- mydfs$d2 %>% 
  full_join(mydfs$d3, by = c('rowname'))

I will have a list of 3 data frames and either some or all of them could be null. I would like to join them, all 3, where they are not null else just ignore. Where all 3 are null, just return null.

Is there a way that I can tell r ‘Combine these 3 data frames where they exist and where they do full join on ‘rowname”?

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

>Solution :

We may remove the NULL elements and then do a join

library(dplyr)
library(purrr)
mydfs %>% 
    discard(is.null) %>% 
    reduce(full_join, by = "rowname")
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