I'd like to calculate the pairwise distance between two
geometry columns. That is, I want to calculate the distance between several pairs of geometries without calculating the full distance matrix between every possible pair. I've come up with a way to do it with
purrr::map2_dbl, but it feels messy. Is there a better approach?
library(sf) library(purrr) library(dplyr) # Load sample data and project to EPSG:5070. nc <- st_transform(crs = "+init=epsg:5070", st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)) # Here's the full distance matrix. I don't want that. nc_dist_mat <- st_distance(nc, nc) # Define a function to calculate distances pairwise: pairwise_distance <- purrr::partial(purrr::map2_dbl, .f = sf::st_distance) # distance to self is always zero: pairwise_distance(st_geometry(nc), st_geometry(nc)) # distance to random county isn't always zero: pairwise_distance(st_geometry(nc), st_geometry(dplyr::sample_frac(nc)))