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

Time:02-05

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

CodePudding user response:

We may remove the NULL elements and then do a join

library(dplyr)
library(purrr)
mydfs %>% 
    discard(is.null) %>% 
    reduce(full_join, by = "rowname")

If we want to avoid the join if all the elements are NULL or the number of rows (NROW) is 0, then can use a if/else conditon

mydfs %>% 
      discard(~ is.null(.x)|NROW(.x) == 0 ) %>% 
    {if(length(.) < 2) .  else {.} %>%
        reduce(full_join, by = "rowname")
  }
  •  Tags:  
  • Related