I have the following two data frames:
df1 <- data.frame(ID = c("A","A","B","B","C","D","D","D","E"),
Date = as.POSIXct(c("2018-04-12 08:56:00","2018-04-13 11:03:00","2018-04-14 14:30:00","2018-04-15 03:10:00","2018-04-16 07:28:00","2018-04-17 11:17:00","2018-04-17 14:21:00","2018-04-18 09:56:00","2018-05-02 07:49:00")))
df2 <- data.frame(ID = c("A","A","A","B","C","D","D","D","D","D","E"),
Date = as.POSIXct(c("2018-04-10 07:11:00","2018-04-11 18:59:00","2018-04-12 12:37:00","2018-04-15 01:43:00","2018-04-21 09:52:00","2018-04-15 20:25:00","2018-04-17 12:33:00","2018-04-17 14:21:00","2018-04-18 10:59:00","2018-04-20 14:11:00","2018-05-01 09:50:00")))
For df1, I would like to do 2 things: First, I want to find the nearest preceding date, by ID, from df2. Second, I want to find the nearest following date, by ID, from df2, again without repeating values. In both cases, I do not want dates from df2 to be repeated in df1.
Using the roll = Inf feature from the data.table package I am able to merge in the preceding dates by ID.
setDT(df1)
setDT(df2)
setkey(df1, ID, Date)
setkey(df2, ID, Date)[, PrecedingDate:=Date]
result <- df2[df1, roll=Inf]
I'm unsure of how I can pull the nearest following date from df2 into df1, and how I can ensure that dates are not repeated.
The result should be as follows:
result <- data.frame(ID = c("A","A","B","B","C","D","D","D","E"),
Date = as.POSIXct(c("2018-04-12 08:56:00","2018-04-13 11:03:00","2018-04-14 14:30:00","2018-04-15 03:10:00","2018-04-16 07:28:00","2018-04-17 11:17:00","2018-04-17 14:21:00","2018-04-18 09:56:00","2018-05-02 07:49:00")),
PrecedingDate = as.POSIXct(c("2018-04-11 18:59:00","2018-04-12 02:37:00",NA,"2018-04-15 01:43:00",NA,"2018-04-15 20:25:00","2018-04-17 14:21:00",NA,"2018-05-01 09:50:00")),
FollowingDate = as.POSIXct(c("2018-04-12 02:37:00",NA,"2018-04-15 01:43:00",NA,"2018-04-21 09:52:00","2018-04-17 12:33:00","2018-04-17 14:21:00","2018-04-18 10:59:00",NA)))
Any help here would be most appreciated.
df2has the same date asdf1? Is it classified as preceding or following or ignored? - ShreePrecedingDateand the 1stFollowingDateinresultare incorrect imo. They should be both2018-04-12 12:37:00. I've corrected that in my answer. - Jaap