Skip to content
Prev 261736 / 398502 Next

complex search between dataframes

On Jun 2, 2011, at 1:42 PM, Filippo Beleggia wrote:

            
See ?findInterval

Coerce data1 into a matrix, so that the interval boundaries are in increasing order by columns, which is then actually used by findInterval as a vector (eg. c(1, 10, 12, ...)):
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
from    1   12   16   40   55   81  101
to     10   13   23   45   67   99  123


findInterval() will return the interval indices for each data2$position value within the sorted intervals. Since your actual intervals are discontinuous, you only want the values that fit in the odd intervals, which is where the use of %in% seq(1, 13, 2) comes in. Prior to that, findInterval() returns:
[1]  1  4  5  8 14 14 14 14 11

With it:
[1]  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE

Now you can use the TRUE values to index data2$name:
[1] 1 3 9

or data2$position:
[1]  2 20 85



HTH,

Marc Schwartz