Search code examples
rgeospatialdistancelatitude-longitudegeosphere

Trying to find the distance between pairs of latitude and longitude coordinates


Currently have a data frame of 4 columns, "lon.x," "lat.x," "lon.y," "lat.y." with 581 rows. I would like to find the distance between each pair of coordinates.

I tried:

library(geosphere)
distm(c(coords$lon_x, coords$lat_x),c(coords$lon_y, coords$lat_y), fun = distHaversine())

But got this error

Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2

Is there a different way to do this?


Solution

  • Using distm(cbind(coords$lon_x, coords$lat_x), cbind(coords$lon_y, coords$lat_y), distHaversine) or distm(coords[, c("lon_x", "lat_x")], coords[, c("lon_y", "lat_y")], distHaversine) works for me, or even more succinct using with:

    library(geosphere)
    res <- with(coords, distm(cbind(lon_x, lat_x), cbind(lon_y, lat_y), distHaversine))
    res
    #           [,1]     [,2]     [,3]     [,4]      [,5]
    # [1,]  82926.65 102777.0 416520.9 317338.5 229201.72
    # [2,] 227148.47 371663.8 472784.4 441059.9  51871.09
    # [3,] 131700.50 212885.3 344615.2 270580.8 166672.80
    # [4,] 170629.69 314137.0 566038.0 506409.5 114399.77
    # [5,] 125941.89 208759.0 350108.9 275223.3 164881.02
    

    You just want the diag, though.

    diag(res)
    # [1]  82926.65 371663.76 344615.24 506409.47 164881.02
    

    Check:

    with(coords, distm(cbind(lon_x, lat_x)[1,], cbind(lon_y, lat_y)[1,], distHaversine))
    #          [,1]
    # [1,] 82926.65
    with(coords, distm(cbind(lon_x, lat_x)[2,], cbind(lon_y, lat_y)[2,], distHaversine))
    #          [,1]
    # [1,] 371663.8
    with(coords, distm(cbind(lon_x, lat_x)[3,], cbind(lon_y, lat_y)[3,], distHaversine))
    #          [,1]
    # [1,] 344615.2
    

    Toy data:

    n <- 5
    set.seed(42)
    coords <- data.frame(id=seq(n), 
                         lon_x=rnorm(n, 47.364842), lat_x=rnorm(n, 8.573026),
                         lon_y=rnorm(n, 47.364842), lat_y=rnorm(n, 8.573026),
                         sth_else=rnorm(n))