Calculating the distance between points in different data frames

前端 未结 2 456
心在旅途
心在旅途 2020-12-11 09:52

I am trying to find the distance between points in two different data frames given that they have the same value in one of their columns.

I figure the first step is

2条回答
  •  长情又很酷
    2020-12-11 10:34

    For calculating the distance between lat/long points, you can use the distm function from the geosphere package. Within this function you can use several formula's for calculating the distance: distCosine, distHaversine, distVincentySphere and distVincentyEllipsoid. The last one is considered the most accurate one (according to the package author).

    library(geosphere)
    
    A <- data.frame(Lat=1:4, Long=1:4, Name=c("a","b","c","d"))
    B <- data.frame(Lat=5:8, Long=5:8, Name=c("a","b","c","d"))
    
    A$distance <- distVincentyEllipsoid(A[,c('Long','Lat')], B[,c('Long','Lat')])
    

    this gives:

    > A
      Lat Long Name distance
    1   1    1    a 627129.5
    2   2    2    b 626801.7
    3   3    3    c 626380.6
    4   4    4    d 625866.6
    

    Note that you have to include the lat/long columns in the order of first longitude and then latitude.


    Although this works perfectly on this simple example, in larger datasets where the names are not in the same order, this will lead to problems. In that case you can use data.table and set the keys so you can match the points and calculate the distance (as @MichaelChirico did in his answer):

    library(data.table)
    A <- data.table(Lat=1:4, Long=1:4, Name=c("a","b","c","d"), key="Name")
    B <- data.table(Lat=8:5, Long=8:5, Name=c("d","c","b","a"), key="Name")
    
    A[B,distance:=distVincentyEllipsoid(A[,.(Long,Lat)], B[,.(Long,Lat)])]
    

    as you can see, this gives the correct (i.e., the same) result as in the previous method:

    > A
       Lat Long Name distance
    1:   1    1    a 627129.5
    2:   2    2    b 626801.7
    3:   3    3    c 626380.6
    4:   4    4    d 625866.6
    

    To see what key="Name" does, compare the following two datatables:

    B1 <- data.table(Lat=8:5, Long=8:5, Name=c("d","c","b","a"), key="Name")
    B2 <- data.table(Lat=8:5, Long=8:5, Name=c("d","c","b","a"))
    

    See also this answer for a more elaborate example.

提交回复
热议问题