0

I wanted to calculate the distance between two zip codes. Is there any particular function to find the distance?

I used this code

Dataframe <- transform(dataframe, Distance = ifelse(a == "" | b == "", "", georoute( c("a", "b"), verbose=TRUE, returntype="distance", service="bing" )))

Where a and b are two separate columns which contain zipcodes of two different areas.

I used the code given at Determing the distance between two ZIP codes (alternatives to mapdist)

Community
  • 1
  • 1
Ashwan
  • 13
  • 1
  • 4

1 Answers1

0

You can find online a table that has longitudes and latitudes for 29,000+ US cities, along with their zip codes. with it you could then convert your columns of zip codes to coordinates.

Then, the geosphere package offers eight functions for calculating distances (great circle, Haversine, Rhumb, etc) based on coordinates. It also converts meters to miles.

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • Can you please elaborate on the function to be used. I just need the Distances between two points(distance by car) to be used in the dataframe for other purpose. I have calculated the Latitude and Longitude. The function which u mentioned, are used for plotting i guess (Please ignore if im wrong im new to R) – Ashwan Dec 10 '14 at 10:52
  • Read the pdf of the geosphere package and decide which calculation of distance makes sense for you. I do not think driving distance is included, by the way, but you may or may not actually need that. Yes, they are used for plotting, but also can be independent. – lawyeR Dec 10 '14 at 20:30
  • I Used different functions to caluclate the distance and there was a huge difference in the values can u suggest which is accurate 1. 'mapdist('28202', '98032', mode = 'driving')' **from to m km miles seconds minutes hours 28202 98032 4507924 4507.924 2801.224 145935 2432.25 40.5375** 2. 'distHaversine(c(-80.84419, 35.22719), c(-122.25932, 47.38825))' **3666598** 3. 'distVincentySphere(c(-80.84419, 35.22719), c(-122.25932, 47.38825))' **3666598** – Ashwan Dec 11 '14 at 06:55
  • I do not know. You can always write the package maintainer, send your data in a reproducible fashion and they are usually delighted to hear from users. BTW, if the answer has been useful, would you accept it? – lawyeR Dec 11 '14 at 10:53