2

I have the following dataframes. I cannot find a satisfying way to merge them all. I tried the following:

a=data.frame('name'=c('a','b','c','d'),'value'=c(23,45,23,56))
b=data.frame('name'=c('a','b','f','e'),'value'=c(23,45,23,56))
d=data.frame('name'=c('g','b','f','e'),'value'=c(23,45,23,56))
testlist=list(a,b,d)
c=join_all(testlist,by='name',type='left',match='all')

But this return me the following table:

  name value value value
1    a    23    23    NA
2    b    45    45    45
3    c    23    NA    NA
4    d    56    NA    NA

while instead, I would like a table with all the possible names in the first column ('a','b','c','d','e','f','g') and all the other values in the corresponding rows in 3 different columns (1 per dataframe). How can I do it? Does not matter if all the blanck values are NA, I will change then later with 0

Thanks!!!!!!

Jordan
  • 35
  • 5

1 Answers1

2

A base R option is to use Reduce with merge(..., all = TRUE)

Reduce(function(x, y) merge(x, y, by = "name", all = T), testlist)
#  name value.x value.y value
#1    a      23      23    NA
#2    b      45      45    45
#3    c      23      NA    NA
#4    d      56      NA    NA
#5    e      NA      56    56
#6    f      NA      23    23
#7    g      NA      NA    23

Or similarly using dplyr::full_join and purrr::reduce

library(dplyr)
library(purrr)
reduce(testlist, full_join, by = "name")
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68