2017-09-13 84 views
1

我試圖將兩個包含來自多個數據記錄器的溫度數據的大型數據幀結合起來。數據記錄器和列名在每個數據框中都是相同的。一個數據幀包含比另一個數據幀更新的值。如何根據重疊的日期值追加數據框?

的數據基本上是這樣的:

date.time   date   temp1 temp2 
2011-08-22 19:00 2011-08-22 11.265 5.562 
2011-08-22 20:00 2011-08-22 11.254 6.541 
2011-08-22 22:00 2011-08-22 12.256 5.456 
2011-08-22 23:00 2011-08-22 13.568 15.265 

date.time<-c("2011-08-22 19:00", "2011-08-22 20:00", "2011-08-22 22:00","2011-08-22 23:00") 
date<-c("2011-08-22","2011-08-22","2011-08-22","2011-08-22") 
temp1<-c(11.265,11.254,12.256,13.568) 
temp2<-c(5.562,6.541,5.456,15.265) 
df_old<-data.frame(date.time,date,temp1,temp2) 

和:

date.time   date   temp1 temp2 temp3 
2011-08-22 22:00 2011-08-22 12.256 5.456 24.598 
2011-08-22 23:00 2011-08-22 13.568 15.265 11.265 
2011-08-22 24:00 2011-08-22 11.275 5.563 25.295 
2011-08-23 00:00 2011-08-23 11.254 6.544 24.689 

date.time<-c("2011-08-22 22:00", "2011-08-22 23:00", "2011-08-22 24:00","2011-08-23 00:00") 
date<-c("2011-08-22","2011-08-22","2011-08-22","2011-08-23") 
temp1<-c(12.256,13.568,11.275,11.254) 
temp2<-c(5.456,15.265,5.563,6.544) 
temp3<-c(24.598,11.265,25.295,24.689) 
df_new<-data.frame(date.time,date,temp1,temp2,temp3) 

我想縱向基於date.time列共同的價值觀走到一起加入這些:

date.time   date   temp1 temp2 temp3 
2011-08-21 19:00 2011-08-22 11.265 5.562 NA 
2011-08-21 20:00 2011-08-22 11.254 6.541 NA 
2011-08-21 22:00 2011-08-22 12.256 5.456 24.598 
2011-08-21 23:00 2011-08-22 13.568 15.265 11.265 
2011-08-21 24:00 2011-08-22 11.275 5.563 25.295 
2011-08-21 00:00 2011-08-23 11.254 6.544 24.689 

我試過用rbind.fill將其中的一個追加到另一個。

df_all<-rbind.fill(df_old, df_new) 

這確實說明了不同的列數。但是,它只是將一個列疊加在一起,而不是將具有共享值的行放在date.time列中。

我試過尋找如何做到這一點,但只發現如何基於共享列中的值並排合併數據框,但沒有如何合併它們時,從上到下,當有重疊值。

謝謝!

回答

1

好像什麼你所描述的是一個合併:

merge(df_old,df_new,by=c("date.time","date","temp1","temp2"),all=TRUE) 

輸出:

  date.time  date temp1 temp2 temp3 
1 2011-08-22 19:00 2011-08-22 11.265 5.562  NA 
2 2011-08-22 20:00 2011-08-22 11.254 6.541  NA 
3 2011-08-22 22:00 2011-08-22 12.256 5.456 24.598 
4 2011-08-22 23:00 2011-08-22 13.568 15.265 11.265 
5 2011-08-22 24:00 2011-08-22 11.275 5.563 25.295 
6 2011-08-23 00:00 2011-08-23 11.254 6.544 24.689 

編輯:

正如@ eipi10建議,另一個選擇做同樣是:

library(dplyr) 

full_join(df_old, df_new) 
+1

也'庫(dplyr); full_join(df_old,df_new)'。 – eipi10