Home > database >  R how to calculate the distance in km between several points that have long and lat
R how to calculate the distance in km between several points that have long and lat

Time:10-01

I have three points with long and lat(three islands, see matrix bellow) and, I would like to calculate the distance in km between them. Here is my matrix (list1):

list1 <- data.frame(longitude = c(-3.0000,
                                   -1.3333,
                                   -6.5667), 
                     latitude = c(59.0000, 
                                  60.3333,  
                                  62.2500),
                      name= c('Orkney', 
                            'Shetlands', 
                               'Faroe'))

I just want to know the distance between the Faroe Islands and the Shetlands, the distance between the shetlands and Orkney and so on and so on... I am a very beginner in R so I am not familiar with the language... can someone help me?

CodePudding user response:

What you're looking for is the distance matrix. It can be done with geodist package

library(geodist)

distance_matrix <- geodist(list1, measure = 'geodesic' )/1000 #converting it to km

#also, check for other measures in the description

colnames(distance_matrix) <- list1$name
rownames(distance_matrix) <- list1$name

Output:

            Orkney Shetlands    Faroe
Orkney      0.0000  175.7363 411.2688
Shetlands 175.7363    0.0000 352.4388
Faroe     411.2688  352.4388   0.0000

CodePudding user response:

These are two possible solutions:

list1 <- data.frame(longitude = c(-3.0000,
                                  -1.3333,
                                  -6.5667), 
                    latitude = c(59.0000, 
                                 60.3333,  
                                 62.2500),
                    name= c('Orkney', 
                            'Shetlands', 
                            'Faroe'))
require(sf)
#> Loading required package: sf
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
st_distance(st_as_sf(list1, coords = 1:2, crs=4326))
#> Units: [m]
#>          [,1]     [,2]     [,3]
#> [1,]      0.0 175316.8 410279.5
#> [2,] 175316.8      0.0 351338.2
#> [3,] 410279.5 351338.2      0.0
require(geosphere)
#> Loading required package: geosphere
do.call(rbind,lapply(split(list1[,1:2],1:3), distHaversine, p2=list1[,1:2]))
#>       [,1]     [,2]     [,3]
#> 1      0.0 175512.9 410738.5
#> 2 175512.9      0.0 351731.2
#> 3 410738.5 351731.2      0.0

Created on 2021-09-30 by the reprex package (v2.0.1)

Here coords=1:2 indicates the columns that contain the longitude and latitude this can also done by name for more details see ?st_as_sf, crs=4326 stands for the coordinate reference system used (epsg code: https://epsg.io/4326 ). st_distance automatically attaches units. If you want different units the units package has example how to convert to different units.

The second example is slightly less fancy and only works for long lat data since it uses distHaversine (geosphere contains alternative distance functions). lapply is use to calculate each time the distance between one and all other coordinates.

rownames and colnames can be used to attach the names again. The coordinates stay in the same order.

  •  Tags:  
  • r
  • Related