0

Here's what i'm trying to do . Just need to add df2 in df1 as shown below in "Result Should be"

  df1
                   A              B
    1            923955       28-01-2016
    2            226129       28-01-2016
    3            889131       28-01-2016
    4            1047783      28-01-2016
    5            824995       28-01-2016
    6            386925       28-01-2016
df2
               A              B
1            104981       2016-01-28
2            50270        2016-01-28
3            708070       2016-01-28
4            205223       2016-01-28

Result Should be :

masterdf
               A            B              C             D
1            923955       28-01-2016     104981       2016-01-28
2            226129       28-01-2016     50270        2016-01-28
3            889131       28-01-2016     708070       2016-01-28
4            1047783      28-01-2016     205223       2016-01-28
5            824995       28-01-2016       NA            NA
6            386925       28-01-2016       NA            NA
mtoto
  • 23,919
  • 4
  • 58
  • 71
Pankaj Kaundal
  • 1,012
  • 3
  • 13
  • 25
  • `merge(df1, df2, by = "A", all = TRUE)`, then change the name. Or why don't you change the names of `df2` before merging? – Kota Mori Jan 29 '16 at 12:50

1 Answers1

3

Seems like you're not really merging but cbinding two dataframes with different number of rows. If that's the case you can:

# get difference in rows between df's
extra.rows <- nrow(df1) - nrow(df2)

# add the difference in rows to df2 and fill them with NA's
df2[nrow(df2) + extra.rows,] <- NA

# cbind them together
cbind(df1,df2)
#        A          B      A          B
#1  923955 28-01-2016 104981 2016-01-28
#2  226129 28-01-2016  50270 2016-01-28
#3  889131 28-01-2016 708070 2016-01-28
#4 1047783 28-01-2016 205223 2016-01-28
#5  824995 28-01-2016     NA       <NA>
#6  386925 28-01-2016     NA       <NA>
mtoto
  • 23,919
  • 4
  • 58
  • 71