[R] computing distance in miles or km between 2 street addre

Duncan Murdoch murdoch at stats.uwo.ca
Fri Sep 7 01:53:38 CEST 2007


On 06/09/2007 6:17 PM, (Ted Harding) wrote:
> On 06-Sep-07 18:42:32, Philip James Smith wrote:
>> Hi R-ers:
>>
>> I need to compute the distance between 2 street addresses in
>> either km or miles. I do not care if the distance is a "shortest
>> driving route" or if it is "as the crow flies."
>>
>> Does anybody know how to do this? Can it be done in R? I have
>> thousands of addresses, so I think that Mapquest is out of the
>> question!
>>
>> Please rely to: philipsmith at alumni.albany.edu
>>
>> Thank you!
>> Phil Smith
> 
> That's a somewhat ill-posed question! You will for a start
> need a database of some kind, either of geographical locations
> (coordinates) of street addresses, or of the metric of the
> road network with capability to identify the street addresses
> in the database.
> 
> If it's just "as the crow flies", then it can be straightforwardly
> computed in R, either by Pythogoras (when they are not too far
> apart) or using a function which takes account of the shape of
> the Earth,
> 
> There are many R packages which have to do with mapping data.
> Search for "map" through the list of R packages at
> 
> http://finzi.psych.upenn.edu/R/library/maptools/html/00Index.html
> 
> -- maptools in particular. Also look at (for instance) aspace.
> 
> For "shortest driving route" then you need to find the shortest
> distance through a network. You may find some hints in the
> package optim -- but there must be some R experts out there
> on this sort of thing!
> 
> However, the primary need is for the database which gives
> the distance information in one form or another. What were
> you proposing to use for this? As far as I know, R has no
> database relevant to street addresses!

But if the addresses are in the US and include zip codes, a database is 
available online for free, here:

http://www.aggdata.com/files/zip_codes.zip

This includes latitude and longitude.  Zip codes cover a fairly large 
area so this won't give very accurate information, but it's easy to get.

Duncan Murdoch



More information about the R-help mailing list