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”?

>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")

Leave a Reply