r - How to append dataframes based on overlapping date values? -
i'm trying combine 2 large data frames containing temperature data several data loggers. data loggers , column names same in each data frame. 1 data frame contains values more recent other data frame.
the data looks this:
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)
and:
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)
i want join these lengthwise based on shared values in date.time column get:
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
i have tried appending 1 other using rbind.fill.
df_all<-rbind.fill(df_old, df_new)
this account differing number of columns. however, adds columns 1 on-top of other instead of putting rows shared values in date.time column together.
i've tried looking how have found how merge data frames side-by-side based on values in shared column not how merge them top-to-bottom when there overlapping values.
thanks!
it seems you're describing merge:
merge(df_old,df_new,by=c("date.time","date","temp1","temp2"),all=true)
output:
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
edit:
as @eipi10 suggested, option same is:
library(dplyr) full_join(df_old, df_new)
Comments
Post a Comment