[R-sig-Geo] How to list neighbors IDs and corresponding frequencies

Roger Bivand Roger.Bivand at nhh.no
Fri Feb 23 22:38:11 CET 2007


On Fri, 23 Feb 2007, Takatsugu Kobayashi wrote:

> Rusers:
> 
> I am trying to compute a Moran's I coefficient for census tracts for 
> Indianapolis in R. I have a shapefile for Indianapolis and uploaded it 
> into R. I understand how to compute a Moran's I coefficient using knn2nb 
> command. How can I compute a Moran's I based on a edge/border contiguity 
> matrix?

These are functions in the spdep package, yes?

For boundary contiguity based in a shapefile of tract boundaries, see 
poly2nb(), like knn2nb(knearneigh()) for k-nearest neighbours, and 
dnearneigh() for distance-based neighbours.

> 
> Also, in Matlab, using the find command, I can find the IDs for 
> observations that satisfy some conditions like
> 
> a=randn(10,1) % create a row vector that contains 10 random normal numbers
> INDEX=find(a>0) % INDEX is a row vector that contains IDs of 
> observations whose values are greater than 0
> a(INDEX) % Returns frequencies attached to the IDs of observations whose 
> values are greater than 0
> 

a <- rnorm(10)
INDEX <- which(a > 0)
a[INDEX]

I'm not bilingual, but it looks like which(); please use dnearneigh() to
generate objects of class "nb".

It is usually helpful to illustrate your question using an example from 
the help pages - in your case:

example(moran.test)

and

example(poly2nb)

would give you some ideas that should help (the example() function runs 
the example for the named function on packaged data). Trying to work out 
what to do on the packaged data is easier than on your own data, and it is 
easier for people on the list to help if they can see both the code and 
the data (so packaged data avoids the need to post your own until needed, 
if the specific data are the problem).

Hope this helps,

Roger

> This way, I can identify nearest neighbors of a point within n miles.
> 
> Thank you very much
> 
> Takatsugu
> Indiana Univ.
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list