I have two dataframes, x and y, both of which represent items and their respective locations (which are represented as integers). Dataframe x responds to Genes and their locations; dataframe y responds to Enhancers and their locations. For every Gene in dataframe x, I want to find the Enhancer in y whose location is closest. Here are the first five rows of both dataframes:
Gene: | Location:
----------------------------------
CORT | 10450031
LOC107985174 | 110639954
LOC105369199 | 120963648
CD1D | 158178030
EPHA2 | 16124337
Enhancer: | Location:
-----------------------------------------------------
genic|NC_000001.11:180541-181713 | 180541
genic|NC_000001.11:819650-823755 | 819650
genic|NC_000001.11:1290023-1294341 | 1290023
genic|NC_000001.11:2072541-2076498 | 2072541
genic|NC_000001.11:2132164-2134268 | 2132164
I have been using which.min() like so: Enhancers[which.min(abs(x-Enhancers$location)),] where x corresponds to the given gene's location, which does appear to work, but it requires manually inputing the location of each individual gene. I am wondering if there is a way to accomplish this for all the genes at once. Any help would be appreciated, thank you.