Merge 2 dataframes if value within range

There is a package in Bioconductor called IRanges that does what you want.

First, form an IRanges object for your zones:

zone.ranges <- with(zones, IRanges(ZoneStart, ZoneEnd))

Next, find the overlaps:

zone.ind <- findOverlaps(fixes$StartPosition, zone.ranges, select="arbitrary")

Now you have indices into the rows of the zones data frame, so you can merge:

fixes$Zone <- zones$Zone[zone.ind]

Edit: Just realized you have floating point values, while IRanges is integer-based. So you would need to multiply the coordinates by 100, given your precision.

Leave a Comment