I have a set of longitude/latitude points in a data frame called person_location
+----+-----------+-----------+
| id | longitude | latitude |
+----+-----------+-----------+
| 1 | -76.67707 | 39.399754 |
| 2 | -76.44519 | 39.285084 |
| 3 | -76.69402 | 39.36958 |
| 4 | -76.68936 | 39.369907 |
| 5 | -76.58341 | 39.357994 |
+----+-----------+-----------+
I then have another set of longitude and latitude points in a data frame called building_location
:
+----+------------+-----------+
| id | longitude | latitude |
+----+------------+-----------+
| 1 | -76.624393 | 39.246464 |
| 2 | -76.457246 | 39.336996 |
| 3 | -76.711729 | 39.242936 |
| 4 | -76.631249 | 39.289103 |
| 5 | -76.566742 | 39.286271 |
| 6 | -76.683106 | 39.35447 |
| 7 | -76.530232 | 39.332398 |
| 8 | -76.598582 | 39.344642 |
| 9 | -76.691287 | 39.292849 |
+----+------------+-----------+
What I'm trying to do is calculate for each ID within person_location
, what the closest ID is within building_location
. I know how to calculate the difference between two separate points using the distHaversine
function from library(geosphere)
, but how would I get it to evaluate the closest distance from one point to a set of multiple points?