如何加入/排序没有ID的两个相关数据集?

时间:2014-12-10 18:01:42

标签: r merge

我想合并两个相关但没有共同ID的数据框。

示例数据:

 df1                 df2
2010-01-01           2010-01-26
2010-01-02           2010-01-27
2010-01-03           2010-01-28
2010-01-25           2010-01-29
                     2010-01-30
                     (2010-01-31)

渴望:

2010-01-01
2010-01-02
2010-01-03     
2010-01-25 
2010-01-26
2010-01-27
2010-01-28
2010-01-29
2010-01-30

我试过了

library(plyr) # joining tables
hi<-join(df1, df2, by = NULL)

但没有标识符就无法工作。

1 个答案:

答案 0 :(得分:0)

rbind完全符合您的要求:它实际上将列堆叠在一起:

df1 <- data.frame(data=c("2010-01-01", "2010-01-02", "2010-01-03", "2010-01-25"))
df2 <- data.frame(data=c("2010-01-26", "2010-01-27", "2010-01-28", "2010-01-29", "2010-01-30"))

rbind(df1, df2)
#        data
#1 2010-01-01
#2 2010-01-02
#3 2010-01-03
#4 2010-01-25
#5 2010-01-26
#6 2010-01-27
#7 2010-01-28
#8 2010-01-29

如果您的某个数据框的列数多于另一个,请使用rbind.fill包中的plyrNA填充缺失值。

require(plyr)
df1$test <- sample(nrow(df1))
rbind.fill(df1, df2)
#        data test
#1 2010-01-01    3
#2 2010-01-02    1
#3 2010-01-03    2
#4 2010-01-25    4
#5 2010-01-26   NA
#6 2010-01-27   NA
#7 2010-01-28   NA
#8 2010-01-29   NA
#9 2010-01-30   NA