Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Compute distance to all observations in a group


From   Robert Picard <picard@netbox.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Compute distance to all observations in a group
Date   Sat, 25 Feb 2012 11:17:06 -0500

Here's one way to do it using -joinby- to form all pairwise
combinations of stores within each county and using -geodist-
(available from SSC) to calculate the distances:

*----------- begin example -------------
version 12

clear
input county store  double lat double lon
1 1 -18.47785 -70.31808
1 2 -18.47846 -70.31757
1 3 -18.48126 -70.31446
2 1 -20.24057 -70.14498
2 2 -20.23462 -70.14281
2 3 -20.21667 -70.14654
2 4 -20.25699 -70.1329
3 1 -23.67862 -70.40953
3 2 -23.5791 -70.39053
end

tempfile main copy
save "`main'"

rename * *2
rename county2 county
save "`copy'"

use "`main'"
joinby county using "`copy'"

* ssc install geodist
geodist lat lon lat2 lon2, gen(km2near)
drop if store == store2
sort county store km2near store2
keep county store km2near lat lon
by county store: gen near = _n
reshape wide km2near, i(county store) j(near)
order county store lat lon
list, noobs sepby(county)

*------------ end example --------------



On Sat, Feb 25, 2012 at 10:26 AM, Fernando Luco
<flucoestatalist@gmail.com> wrote:
> Dear all,
>
> I have a data set with the coordinates of all stores in a country,
> grouped at the county level. I need to compute the distance from one
> store to all other stores in the same county. In particular, I'm
> interested in computing, for each store, the distance to the nearest
> store, the second one, the third and so on. So far, I haven't been
> able to do so.
> The number of stores varies by county. The data looks like this
>
> County Store  latitude         longitude
> 1            1    -18.47785     -70.31808
> 1             2  -18.47846      -70.31757
> 1             3  -18.48126      -70.31446
> 2            1  -20.24057        -70.14498
> 2            2  -20.23462        -70.14281
> 2            3  -20.21667        -70.14654
> 2            4  -20.25699        -70.1329
> 3            1  -23.67862        -70.40953
> 3            2  -23.5791          -70.39053
>
> Because of what I need to do after getting the distances, I would like
> the data to look as follows
>
> County Store latitude longitude distance_to_nearest_store
> distance_to_second_nearest store ...distance_to_nth_nearest_store.
>
> Any ideas on how to do this?
>
> Thanks,
>
> Fernando
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index