I am trying to calculate the distance between two sets of longitude and latitude coordinates.
I am using the function distm() from the package geosphere to do this.
It works fine if I manually put in the values in the distm() function, but I can't get it to work inside my mutate command.
When running it inside a mutate function I get the error:
Error in mutate_impl(.data, dots) :
Evaluation error: Wrong length for a vector, should be 2.
@Dotpi wrote in a comment "A small note. The method geosphere:distm is not vectorized. To vectorize it use the apply functions." when he replied in this thread (Function to calculate geospatial distance between two points (lat,long) using R)
From that I am guessing that this is what is causing the error in the mutate function, but I don't know how to solve it. I would prefer a tidyverse solution, but any help is appreciated.
Below is a test dataframe with first the code that produces the error, and then a working example where I manually insert the values from the first row in DF.
library(tidyverse)
library(geosphere)
set.seed(1)
DF <- tibble(
Long1 = sample(1:10),
Lat1 = sample(1:10),
Long2 = sample(1:10),
Lat2 = sample(1:10))
DF %>% mutate(
Dist = distm(x= c(Long1, Lat1), y=c(Long2, Lat2), fun = distHaversine ))
distm( x = c(3, 3), y = c(10, 5), fun = distHaversine )
Perhaps we can use pmap
library(purrr)
pmap_dbl(DF, ~ distm(x = c(..1, ..2), y = c(..3, ..4),
fun = distHaversine) %>% c)
When combined with mutate
library(dplyr)
DF %>%
mutate(Dist = pmap_dbl(., ~
distm(x = c(..1, ..2), y = c(..3, ..4), fun = distHaversine)))
# A tibble: 10 x 5
# Long1 Lat1 Long2 Lat2 Dist
# <int> <int> <int> <int> <dbl>
# 1 3 3 10 5 808552.
# 2 4 2 2 6 497573.
# 3 5 6 6 4 248726.
# 4 7 10 1 2 1110668.
# 5 2 5 9 10 951974.
# 6 8 7 8 8 111319.
# 7 9 8 7 9 246730.
# 8 6 4 5 1 351986.
# 9 10 1 3 7 1024599.
#10 1 9 4 3 745867.