I’ve a data frame of time series and values. The time series are seconds from the epoch. Here is how the top few elements look like in that data frame
val = seq(1,19)
ts = seq(1342980888,1342982000,by=60)
x = data.frame(ts = ts,val = val)
head(x)
ts val
1 1342980888 1
2 1342980948 2
3 1342981008 3
4 1342981068 4
5 1342981128 5
6 1342981188 6
I would like to some kind of an interval search function which takes in as input a time stamp say 1342980889 (+1 the the ts in the first row) and it should return 1,2 (the row number) as output. Basically, I want to find the two rows which have time stamps that bracket the input time stamp 1342980889. While this is relatively easy to do using “which”, I suspect “which” does a vector scan and as the real data frame is quite large I want to do it using a binary search. Thanks much in advance
You should use the
findIntervalfunction. It will give you the index of the row wherex$tsis immediately smaller than the value you are looking for (and you just have to add one to get the other index)Also note that the function is vectorized, i.e., the first argument can be a vector of values to look for: