Home > Software engineering >  Calculate minimal distances between two lists of coordinates R
Calculate minimal distances between two lists of coordinates R

Time:11-16

I have two datasets of coordinates that look something like this:

set.seed(0)
coords_A <- data.frame(id = 1:10, 
                         lon = rnorm(10, 50, 2),
                         lat = rnorm(10, 4, 1))
coords_B <- data.frame(id = 1:7, 
                         lon = rnorm(7, 50, 2),
                         lat = rnorm(7, 4, 1))

Now I would like to find the minimum distance (e.g. Haversine) from points in coords_A to any of the points in coords_B. Ending up with something like this (distances are not correct here just random numbers for illustration):

   id      lon      lat   mindist
1   1 49.55146 3.764293 1527.3709
2   2 50.75479 3.457112 1168.2238
3   3 50.26667 3.566690  864.1648
4   4 51.60838 3.350528  750.3870
5   5 49.88579 4.726751  650.0288
6   6 51.00722 5.151912  680.3228
7   7 52.17154 4.992160  530.8654
8   8 48.61809 3.570487 1346.9611
9   9 47.43080 5.238304 1249.6141
10 10 50.09345 3.720654  931.8014

I know how to do this in a non-efficient way by looping over all the points in A, calculating the distance to all points in B and retaining the lowest value but I'm looking for a better solution. Who can point me in the right direction?

CodePudding user response:

Even though the results don't agree, this is how I 'd approach this

m1 <- apply(coords_A[-1], 1, function(i)apply(coords_B[-1], 1, function(j)distHaversine(i, j)))
coords_A$min_dist <- apply(m1, 2, min)

coords_A
   id      lon      lat  min_dist
1   1 52.52591 4.763593 150828.07
2   2 49.34753 3.200991  25659.01
3   3 52.65960 2.852343  77580.36
4   4 52.54486 3.710538  57676.61
5   5 50.82928 3.700785  24770.65
6   6 46.92010 3.588489 294041.37
7   7 48.14287 4.252223 188424.82
8   8 49.41056 3.108079  27308.44
9   9 49.98847 4.435683  53185.00
10 10 54.80931 2.762462 300434.71
  • Related