Reputation: 1
I have 2 datasets with trade times. I am looking for a way to be able to check if for a trade occuring at a certain time in my dataset1, I can find a trade occuring at the same time in my dataset 2 and return the position of this trade in my dataset 2. Furthermore, if there does not exist a corresponding trade in my dataset2, I would like to check if there exists one whithin a time window of 1min, or 2min, or 3min, ...(until it finds it with a maximum of 15 minutes).
dataset1 <- as.POSIXct(c("26/09/1999 09:00", "26/09/1999 09:40", "27/09/2000 10:53"), format="%d/%m/%Y %H:%M")
dataset2 <- as.POSIXct(c("14/08/1999 09:00", "26/09/1999 09:40", "27/09/1999 10:53", "27/09/2000 10:53"), format="%d/%m/%Y %H:%M")
It should return a new vector Match: c(NA, 2, 4)
I tried this:
VectorMatch <- rep(NA, length(les))
for (i in length(les)){
j<-0
while((is.na(VectorMatch[i])) & (j <15)){
VectorMatch[i] <- match((les[i]+j*60),(les2))
j <- j+1
}
}
But it does not seem to work. It gives me NA NA 4 instead of NA 2 4
Thanks a lot for your help
Cheers
Upvotes: 0
Views: 154
Reputation: 4473
My first answer neglected 15min window part altogether, and only addressed exact matching. Following "long" code provides full solution
library(data.table)
dt1 <- data.table(x=dataset1, i=c(1, 2, 3))
dt2 <- data.table(x=dataset2, i=c(1, 2, 3, 4))
setkey(dt1, x)
setkey(dt2, x)
d <- dt1[dt2, roll="nearest"]
d[ , x1:=dt1[d[,i], x], ][ , .(i.i[which.min(x-x1)] , min(difftime(x, x1, units ="mins"))), i][, ifelse(abs(V2) < 15, V1, NA),]
[1] NA 2 4
First answer
This will give you the desired outcome
match(dataset1, dataset2)
[1] NA 2 4
Upvotes: 3