需要组合两个数据帧而不删除任何重复值

时间:2016-01-29 12:43:22

标签: r dataframe

这就是我想要做的。只需要在df1中添加df2,如下面“结果应该是”

所示
  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

结果应该是:

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

1 个答案:

答案 0 :(得分:3)

似乎你并没有真正合并,而是将两个具有不同行数的数据帧绑定在一起。如果是这种情况,你可以:

# 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>