1

I start with list l1, which I want to convert to output. As such, the data frames in l1 need to be merged by their first column (A).

df1 <- read.table(text="
              A B
              1 3
              2 4 ", header=T)
df2 <- read.table(text="
              A C
              1 8
              3 9 ", header=T)

l1 <- list(df1, df2); l1

output <- read.table(text="
              A B C
              1 3  8
              2 4  NA
              3 NA 9 ", header=T); output
milan
  • 4,782
  • 2
  • 21
  • 39

2 Answers2

3
library(purrr)
reduce(l1, merge, by = "A", all = TRUE)

This will work also in case if there is more then two data frames in the list.

Iaroslav Domin
  • 2,698
  • 10
  • 19
0

For a baseR option, try using merge() in outer join mode:

output <- merge(df1, df2, by = "A", all = TRUE)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360