Distance matrix geographic points (capital cities)
<p>I'm looking to create a distance matrix of the capital cities of countries around the world. I have created a distance matrix of country centroids like this. Shapefile is derived from <a href="http://www.gadm.org/version2" rel="nofollow noreferrer">http://www.gadm.org/version2</a>:</p>

<pre><code>library(rgeos)
library(rgdal)
shapefile &lt;- readOGR("./Map/World Map", layer = "TM_WORLD_BORDERS-0.3")

centroids &lt;- gCentroid(shapefile, byid = TRUE, as.character(shapefile@data$UN)
dist_matrix &lt;- as.data.frame(geosphere::distm(centroids)
colnames(dist_matrix) &lt;- shapefile@data$NAME
rownames(dist_matrix) &lt;- shapefile@data$NAME
</code></pre>

<p>Now I want to do the same thing but with country capitals instead of country centroids. Ideally I want a method that works even if I have other geographic points that aren't capitals. So far I have made it to plotting the capitals on the given shapefile but I can't find a way to create a distance matrix out of them. </p>

<pre><code>library(rgeos)
library(rgdal)
library(maps)
library(tidyverse)
shapefile &lt;- readOGR("./Map/World Map", layer = "TM_WORLD_BORDERS-0.3")

data("world.cities)
world..cities &lt;- world.cities %&gt;%
filter(world.cities$capital == 1)
plot(shapefile)
points(world.cities$long, world.cities$lat, col ="red", cex = .6, pch = 22, add = TRUE)
</code></pre>

<p>The ideal output format would look something like this</p>

<pre><code>         Algeria  Albania  Azerbaijan
Algeria     0        x          x
Albania     x        0          x
Azerbaijan  x        x          0
</code></pre>

<p>Where x denotes the distance between the capitals of the respective countries in metres or kilometres. </p>