0

With data frames like below,

first <- data.frame(x0=1:5,
                    x1=rnorm(5),
                    x2=c("M","F","M","F","F"))

second <- data.frame(x0=10:14,
                     x1=rnorm(5),
                     x3=c("M","F","M","F","F"))

Expect a dataframe that holds columns in both dataframes and have NA for the columns missing in each other.

+----+-----+-----+-----+
| x0 |  x1 |  x2 |  x3 |
+----+-----+-----+-----+

Could have used rbind function, however that requires same columns to be in the two dataframes.

user3206440
  • 4,749
  • 15
  • 75
  • 132

1 Answers1

1

Maybe rbind.fill() from plyr can help:

library(plyr)
#Data
first <- data.frame(x0=1:5,
                    x1=rnorm(5),
                    x2=c("M","F","M","F","F"))

second <- data.frame(x0=10:14,
                     x1=rnorm(5),
                     x3=c("M","F","M","F","F"))
#Join
third <- rbind.fill(first,second)

   x0          x1   x2   x3
1   1  0.42646422    M <NA>
2   2 -0.29507148    F <NA>
3   3  0.89512566    M <NA>
4   4  0.87813349    F <NA>
5   5  0.82158108    F <NA>
6  10  0.68864025 <NA>    M
7  11  0.55391765 <NA>    F
8  12 -0.06191171 <NA>    M
9  13 -0.30596266 <NA>    F
10 14 -0.38047100 <NA>    F
Duck
  • 39,058
  • 13
  • 42
  • 84