Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Identify Neighbor Regions with Latitudes and Longitudes
From
Brendan Halpin <[email protected]>
To
[email protected]
Subject
Re: st: Identify Neighbor Regions with Latitudes and Longitudes
Date
Sun, 23 Sep 2012 21:35:02 +0100
On Sun, Sep 23 2012, ��wrote:
> Hello, Brendan,
>
> Thanks a lot for replaying. With distance matrix, I am not quite
> understand how to figure up which cities are the neighbor cities for a
> particular cities. Are you talking about the nearest city? I need all
> the adjoining cities for each one.
If you can define clearly what you mean by "adjoining", this is easy
enough. Say you want to identify the nearest three cities to each city.
If your data has a row for each city with distances to each other city,
you can identify the nearest N cities easily enough:
----------------------------------------------------------------------
// Data containing city id and distance to each of 10 cities
// Note distance to self is included
input id d1 d2 d3 d4 d5 d6 d7 d8 d9 d10
1 0 89 83 132 165 19 151 14 153 134
2 89 0 54 159 73 165 69 94 46 157
3 83 54 0 95 113 72 64 94 34 86
4 132 159 95 0 75 80 174 91 80 98
5 165 73 113 75 0 50 127 138 119 150
6 19 165 72 80 50 0 89 67 36 99
7 151 69 64 174 127 89 0 99 129 91
8 14 94 94 91 138 67 99 0 113 47
9 153 46 34 80 119 36 129 113 0 104
10 134 157 86 98 150 99 91 47 104 0
end
// Reshape long
reshape long d, i(id) j(city)
// Sort by distance within ID, keep the lowest three distances to other cities
sort id d
drop if id==city
by id: keep if _n<=3
// Re-initialise the reshape j-counter and reshape wide
by id: gen t = _n
reshape wide d city, i(id) j(t)
----------------------------------------------------------------------
Brendan
--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147
mailto:[email protected] ULSociology on Facebook: http://on.fb.me/fjIK9t
http://teaching.sociology.ul.ie/bhalpin/wordpress twitter:@ULSociology
*
* 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/