I have two sets of (sorted) POSIXct time series:
set.seed(123)
ll = sort(strptime("16/07/2015", format="%d/%m/%Y") + 10*3600 + 1:3600 + round(rnorm(3600), digits=3))
tt = sort(strptime("16/07/2015", format="%d/%m/%Y") + 10.2*3600 + 1:180*10 + round(rnorm(180), digits=3))
tplus = 0:180
where it llactually has some observations 10 ^ 5, tt10 ^ 3 - 10 ^ 4 and tplushas a length of 10 ^ 3. From ttI am building a matrix of timestamps tt1, adding tplusto each observation in tt:
tt1 = t(sapply(tt, function(x) x+tplus))
For each of these timestamps, I need to know what was the last observation ll(as an index ll). I can calculate this as:
tt2 = apply(tt1, c(1,2), function(x) max(which(ll <= x)))
but it’s slow, and I have to do this calculation about 10 ^ 3 times, so how can I speed it up? Given that it is llsorted and tt1sorted by column as well as by row, I was hoping something could exist.
Look at the data:
> head(ll)
[1] "2015-07-16 10:00:00.440 CEST" "2015-07-16 10:00:01.769 CEST" "2015-07-16 10:00:04.071 CEST" "2015-07-16 10:00:04.559 CEST"
[5] "2015-07-16 10:00:05.128 CEST" "2015-07-16 10:00:06.734 CEST"
> head(tt1[,1:4])
[,1] [,2] [,3] [,4] ...
[1,] 1437034330 1437034331 1437034332 1437034333
[2,] 1437034341 1437034342 1437034343 1437034344
[3,] 1437034350 1437034351 1437034352 1437034353
[4,] 1437034359 1437034360 1437034361 1437034362
[5,] 1437034371 1437034372 1437034373 1437034374
[6,] 1437034381 1437034382 1437034383 1437034384
And the expected result:
> head(tt2)
[,1] [,2] [,3] [,4] ...
[1,] 729 729 731 732
[2,] 740 741 742 743
[3,] 748 749 751 752
[4,] 759 760 760 762
[5,] 770 772 773 774
[6,] 780 781 783 785