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')])