Finding the distance between Zip Codes

Hi All,

Hope you are doing well!..I am trying to find the distance between the origin and the destination zip codes...Can you please help me here on how to achieve this with R...Please find the data below:

tibble::tribble(
~Origin.Zip, ~Destination.Zip,
"11235-5262", 8638,
"32714-4682", 8810,
"63031", 76140,
"85262-3135", 89015,
"75240-5654", 8638,
"93730-5151", 98443,
"47172-7801", 91761,
"92651-3471", 95691,
"11731-1623", 8638,
"95003-9613", 91730,
"12590", 77047,
"92122-1707", 11735,
"32812", 92865,
"77477", 91761,
"86001-5590", 59930,
"86001-5590", 59930,
"10901-7854", 8638,
"07302-2871", 8817,
"92657-1307", 92865,
"57252-5968", 91789,
"94588-3957", 77047,
"91001-2733", 1373,
"78572-8222", 91352,
"28348", 91752,
"73801", 28610
)
Thanks,
Arun

Maybe something like this.
Notes:

  1. I have never used the geosphere package.
  2. I found the file of ZIP code latitude and longitude with a quick web search. I have no information about its quality
  3. I changed your tibble so that Destination.Zip is a character column and I added a leading zero to cases where the code as given only has four digits.
library(tidyr)
library(dplyr)
DF <- tibble::tribble(
  ~Origin.Zip, ~Destination.Zip,
  "11235-5262", "08638",
  "32714-4682", "08810",
  "63031", "76140",
  "85262-3135", "89015",
  "75240-5654", "08638",
  "93730-5151", "98443",
  "47172-7801", "91761",
  "92651-3471", "95691",
  "11731-1623", "08638",
  "95003-9613", "91730",
  "12590", "77047",
  "92122-1707", "11735",
  "32812", "92865",
  "77477", "91761",
  "86001-5590", "59930",
  "86001-5590", "59930",
  "10901-7854", "08638",
  "07302-2871", "08817",
  "92657-1307", "92865",
  "57252-5968", "91789",
  "94588-3957", "77047",
  "91001-2733", "01373",
  "78572-8222", "91352",
  "28348", "91752",
  "73801", "28610"
)
DF <- separate(DF, Origin.Zip, into = c("Zip", "Route"))

#Data on ZIP code Lat. and Long. from https://gist.github.com/erichurst/7882666
ZIPS <- read.csv("US Zip Codes from 2013 Government Data", 
                 colClasses = c("ZIP" = "character"))

DF <- inner_join(DF, ZIPS, by = c("Zip" = "ZIP"))
DF <- rename(.data = DF, LAT.Orig = LAT, LNG.Orig = LNG)
DF <- inner_join(DF, ZIPS, by = c("Destination.Zip" = "ZIP")) 
DF
Orig <- as.matrix(select(DF, LNG.Orig, LAT.Orig))
Dest <- as.matrix(select(DF, LNG, LAT)) 

DF$Distance <- geosphere::distVincentyEllipsoid(p1 = Orig, p2 = Dest))
2 Likes

@FJCC, Really appreciate your response!..I am checking this and will get back to you!...

Thanks,
Arun

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.