Josh Josh - 1 month ago 5
R Question

Find Distance Between Locations in R Data Frame

I've got a dataframe with the following rows and columns and I'd like to find the distance between county_a and county_b

county_a county_b lat_a lon_a winner_a lat_b lon_b winner_b
1 01001 01001 32.536382 -86.644490 rep 32.536382 -86.644490 rep
2 01003 01001 30.659218 -87.746067 rep 32.536382 -86.644490 rep
3 01005 01001 31.870670 -85.405456 rep 32.536382 -86.644490 rep
4 01007 01001 33.015893 -87.127148 rep 32.536382 -86.644490 rep
5 01009 01001 33.977448 -86.567246 rep 32.536382 -86.644490 rep
6 01011 01001 32.101759 -85.717261 dem 32.536382 -86.644490 rep


I've tried the following and got an error (both below):

library(geosphere)
library(RJDBC) # Not sure this was used for this but it comes up earlier in the program
library(dplyr)
df%>%mutate(dist = distm(c(lon_a,lat_a), c(lon_b, lat_b), fun=distHaversine))


error:
Error in eval(substitute(expr), envir, enclos) :
Wrong length for a vector, should be 2


Thanks in advance for the help!

Answer

You need to give the arguments in matrix form with two columns each. So use cbind instead of c:

df <- read.table(text="  county_a county_b     lat_a      lon_a winner_a     lat_b      lon_b winner_b
1    01001    01001 32.536382 -86.644490      rep 32.536382 -86.644490      rep
2    01003    01001 30.659218 -87.746067      rep 32.536382 -86.644490      rep
3    01005    01001 31.870670 -85.405456      rep 32.536382 -86.644490      rep
4    01007    01001 33.015893 -87.127148      rep 32.536382 -86.644490      rep
5    01009    01001 33.977448 -86.567246      rep 32.536382 -86.644490      rep
6    01011    01001 32.101759 -85.717261      dem 32.536382 -86.644490      rep")

library(dplyr)
library(geosphere)
df %>% mutate(dist = distHaversine(cbind(lon_a, lat_a), cbind(lon_b, lat_b)))

This gives you:

  county_a county_b    lat_a     lon_a winner_a    lat_b     lon_b winner_b      dist
1     1001     1001 32.53638 -86.64449      rep 32.53638 -86.64449      rep      0.00
2     1003     1001 30.65922 -87.74607      rep 32.53638 -86.64449      rep 233609.47
3     1005     1001 31.87067 -85.40546      rep 32.53638 -86.64449      rep 138247.91
4     1007     1001 33.01589 -87.12715      rep 32.53638 -86.64449      rep  69929.04
5     1009     1001 33.97745 -86.56725      rep 32.53638 -86.64449      rep 160579.78
6     1011     1001 32.10176 -85.71726      dem 32.53638 -86.64449      rep  99747.13