Using the osrm package, I want to calculate the distance between two points stored in two different columns of a data frame and put it into a new column.
Doing the following, however, does not work and throws the error: object 'lon' not found. What did go wrong here?
pacman::p_load(
tidyverse,
sf,
osrm
)
test_1 <- data.frame(name_1 = c("a", "b"), long = c(13.371871012201932, 13.445856720780322), lat = c(52.5212875734526, 52.503693991115206)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_1 = "geometry")
test_2 <- data.frame(name_2 = c("c", "d"), long = c(13.401224766110927, 13.349212418670756), lat = c(52.51946945798967, 52.5149481833963)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326) %>%
rename(geom_2 = "geometry")
comb <- expand_grid(test_1, test_2)
routes <- comb %>%
mutate(distances = osrmRoute(
src = geom_1,
dst = geom_2,
returnclass = "sf"
)$distance)