简体   繁体   中英

Merging dataframes of different length by matching dates

I have two dataframes of different lengths (n=3012 and 3008) that I need to merge based upon date. I have tried using the merge and rbindfill functions but had no success. The common thread between the two dataframes is date_time however both df2 does not have all of the same values as df1.

df1

ID date_time Q_cfs Data_Code
68 2016-11-01 00:00:00   353         P
69 2016-11-01 00:15:00   356         P
70 2016-11-01 00:30:00   357         P
71 2016-11-01 00:45:00   356         P
72 2016-11-01 01:00:00   358         P
73 2016-11-01 01:15:00   355         P

df2

ID  stage           date_time
1  4.82 2016-11-01 00:00:00
2  4.83 2016-11-01 00:15:00
3  4.84 2016-11-01 00:30:00
4  4.85 2016-11-01 00:45:00
5  4.86 2016-11-01 01:00:00
6  4.87 2016-11-01 01:15:00

I tried using merge (below) but it did not work since the column lengths are different.

DF_New<- merge(df1,df2, by.x = df1$date_time, by.y = df2$date_time)

I also tried using rbind.fill (below) but it replaced all the values of df2 with .

DF_New <- rbind.fill(df1,df2)

Any suggestions?

Use the dplyr package and try left_join() . This returns all rows from df1 and all columns from both df1 and df2 . Any rows in df1 with no match will receive NA .

library(dplyr)
left_join(df1, df2, by = "date_time")

Check out the other types of join you can have with ?join .

You have it almost correct. All you need to do, is to add one more argument into your function, like in the example below:

DF_New<- merge(df1,df2, by.x = df1$date_time, by.y = df2$date_time, all=TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM