-1

I have multiple data frames (different rows and columns) and I am trying to concatenate them into one. They come with a different number of columns but equal names. Simply:

> colnames(data1)
  "A" "B" "C" "D" "E" "F" "G" "H"

> colnames(data2)
  "A" "B" "C" "D"

> colnames(data3)
  "A" "D" "E" "F" "H"

I need to concatenate all three data frames into one in a way that match the column name, and if it is not matchable just insert "NA" for that particular column. Thanks in advance

Soheil
  • 193
  • 4
  • 10

1 Answers1

2

Use dplyr::bind_rows:

data1 <- data.frame(a = 1:3)
data2 <- data.frame(a = 4:6, b = 7:9)
data3 <- data.frame(b = 11:13)
dplyr::bind_rows(data1, data2, data3)

#   a  b
#1  1 NA
#2  2 NA
#3  3 NA
#4  4  7
#5  5  8
#6  6  9
#7 NA 11
#8 NA 12
#9 NA 13
Linards Kalvans
  • 479
  • 2
  • 8