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 )