I have two sets of data:
First set:
patient<-c("A","A","B","B","C","C","C","C")
arrival<-c("11:00","11:00","13:00","13:00","14:00","14:00","14:00","14:00")
lastRow<-c("","Yes","","Yes","","","","Yes")
data1<-data.frame(patient,arrival,lastRow)
Another set of data:
patient<-c("A","A","A","A","B","B","B","C","C","C")
availableSlot<-c("11:15","11:35","11:45","11:55","12:55","13:55","14:00","14:00","14:10","17:00")
data2<-data.frame(patient, availableSlot)
I want to create add a column to the first dataset such that for each last row of each patient, it shows the available slot that is closest to the arrival time:
The results would be:
patient arrival lastRow availableSlot
A 11:00
A 11:00 Yes 11:15
B 13:00
B 13:00 Yes 12:55
C 14:00
C 14:00
C 14:00
C 14:00 Yes 14:00
Would appreciate if anyone can tell me how I can implement this in R.
Here is a solution (based on joel.wilson's answer to my question) that will work with base
R
I'd use data.table, first cleaning up by converting to ITime and ignoring redundant rows:
Then you can do a "rolling join":
How it works
The syntax is
x[i, on=, roll=, j]
.on=
are the merge-by columns.i
, we are looking for matches inx
.roll="nearest"
, the final column in theon=
is "rolled" to its nearest match.on=
columns in the original tables can be referenced withx.*
andi.*
prefixes.j
argument should give a list of columns, and.()
is an alias forlist()
here.Check out the package's introductory materials at http://r-datatable.com/Getting-started and type
?data.table
for the docs relevant to rolling joins.I would stop at
res
, but if you really want it back in your original table...Now,
data1
hasavailableSlot
in a new column, similar to when you dodata1$col <- val
.