I am using the nearest function to find nearest genomic range but it doesn't seem to return the correct result when I have a large list for subject.Reproducible example below. Here when using all subjects we get the 14093 as the nearest but actually it should be 14094. When testing just 14093 and 14094 this returns the correct result of 2 e.g. 14094
You are making a mistake, thinking that the order is staying the same.
> prpr[nearest(region, prpr)]
GRanges object with 1 range and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] NC_007077.3 [2966428, 2966429] -
-------
seqinfo: 884 sequences from an unspecified genome; no seqlengths
## Just the two positions
> prpr[14093:14094][nearest(region, prpr[14093:14094])]
GRanges object with 1 range and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] NC_007077.3 [3079949, 3079950] +
-------
seqinfo: 884 sequences from an unspecified genome; no seqlengths
## Bump out to a larger window
> prpr[14075:14125][nearest(region, prpr[14075:14125])]
GRanges object with 1 range and 0 metadata columns:
seqnames ranges strand
<Rle> <IRanges> <Rle>
[1] NC_007077.3 [2966428, 2966429] -
-------
seqinfo: 884 sequences from an unspecified genome; no seqlengths