Is there a slick way to join data tables in R where key values of time are close, but not exactly the same? For example, suppose I have a data table of results that are given for different time periods:
DT1 = data.table(x=rep(c("a","b","c"),each=3), time=c(10,30,60), v=1:9)
Here, we have some values (v) for different categories (x) taken at different times (time). Now suppose that I have data from another source that provides some time values for the different categories:
DT2=data.table(x=rep(c("a","b","c"),each=1),time=c(10,10,60))
I might be interested in trying to match the times in DT2 as closely as I can to DT1 to predict a value, v, for my DT2 categories. I would like to do something like
setkeyv(DT2,c("x","time"))
merge(DT1,DT2,by=c("time","v")
Which returns:
time x v
1: 10 a 1
2: 10 b 4
3: 60 c 9
But what if my times didn't have the same precision? For example:
DT2=data.table(x=rep(c("a","b","c"),each=1),time=c(17,54,3))
Is there a way to perform a similar merge but pick times of DT2 that are close to those of DT1? That is 17 would be close to 30, 54 close to 60, and 3 close to 10?
If this simple example isn't clear, I'll briefly explain the larger problem that I am having. I have a data table with columns: category, time, output1, output2... There are hundreds of categories with associated times. I might want to pull output 1 for all categories at a specific time. Since the times were sampled with no apparent logic, sometimes the time is rounded to the nearest even second; in other cases the times are rounded to the nearest minute or even 10 minutes.
I could write a script to rewrite times in more common format, but I am curious if there is a slick data.table solution that I haven't seen. I have explored the rolling merge with no success.