I want to create a "matrix" of the distance between multiple coordinates with each other. Preferably using dplyr/geosphere. I already saw that the geosphere package offers this. I managed to create the distance between two vectors but I have difficulties creating the full matrix.
This is the sample table with multiple coordinates.
df <- data.frame(latitude = c(49.48609,-8.14671,11.28625),
longitude = c(8.463678,143.05793,-11.18285))
latitude longitude
1 49.48609 8.463678
2 -8.14671 143.057930
3 11.28625 -11.182850
And this is the output I am looking for:
latitude longitude distance-latlon1 distance-latlon2 distance-latlon3
1 49.48609 8.463678 NA *latlon2><latlon1 *latlon3><latlon1
2 -8.14671 143.057930 *latlon1><latlon2 NA *latlon3><latlon2
3 11.28625 -11.182850 *latlon1><latlon3 *latlon2><latlon3 NA
I tried out using geosphere but I only found a way to calculate the distance between two columns (which in this snippet results in a 0).
library(geosphere)
df$distance <- distVincentyEllipsoid(df[,c('longitude','latitude')],
df[,c('longitude','latitude')])
You need the distm
function of the geosphere
-package. With:
# create a distance matrix
m <- distm(df[2:1], df[2:1], fun = distVincentyEllipsoid)
# replace the diagonal with NA
diag(m) <- NA
# make column names for the distance matrix
colnames(m) <- paste0('r',1:nrow(df))
# bind the distance matrix to the dataframe
cbind.data.frame(df, m)
you get:
latitude longitude r1 r2 r3 1 49.48609 8.463678 NA 13792423 4606658 2 -8.14671 143.057930 13792423 NA 17189185 3 11.28625 -11.182850 4606658 17189185 NA
We can use the st_distance
function from the sf
package, which uses functions from geosphere
to calculate the distance if the sf
object is in lon-lat projection (EPSG 4326). df2
is the example output.
# Load packages
library(dplyr)
library(sf)
# Create example data frame
df <- data.frame(latitude = c(49.48609,-8.14671,11.28625),
longitude = c(8.463678,143.05793,-11.18285))
# COnvert to sf object
df_sf <- st_as_sf(df, coords = c("longitude", "latitude"))
# Set the projection as ESPG 4326 (long_lat)
st_crs(df_sf) <- 4326
# Apply the st_distance function
dist_m <- st_distance(df_sf)
# Combine with df
df2 <- df %>%
mutate(`distance-latlon1` = as.numeric(dist_m[, 1]),
`distance-latlon2` = as.numeric(dist_m[, 2]),
`distance-latlon3` = as.numeric(dist_m[, 3]))
# Replace 0 with NA
df2[df2 == 0] <- NA
df2
latitude longitude distance-latlon1 distance-latlon2 distance-latlon3
1 49.48609 8.463678 NA 13792423 4606658
2 -8.14671 143.057930 13792423 NA 17189185
3 11.28625 -11.182850 4606658 17189185 NA
Here is an alternate way to combine dist_m
with df
.
library(tidyr)
# Convert dist_m to data frame
dist_df <- dist_m %>%
as.table() %>%
as_data_frame() %>%
spread(Var2, n) %>%
select(-Var1) %>%
mutate_all(as.numeric) %>%
setNames(paste0("distance-latlon", 1:nrow(df)))
# Combine with df
df2 <- df %>%
bind_cols(dist_df)
# Replace 0 with NA
df2[df2 == 0] <- NA
Instead of distVincentyEllipsoid
I would use the faster and more precise distGeo
df = df[,2:1] # the order should be longitude, latitude!
distm(df, df, distGeo)
or
library(raster)
d <- pointDistance(df)
perhaps followed by
as.matrix(as.dist(d))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.