[R-sig-Geo] get the neighbors from a specified node in an nb object

Roger Bivand Roger.Bivand at nhh.no
Thu Mar 4 17:41:50 CET 2010


On Thu, 4 Mar 2010, Jonathan Thayn wrote:

> I'm sure there is a simple way to do this but I can't seem to find it. I 
> have an nb object and a list of the landcover types associated with each 
> node in the neighborhood. I want to create a table showing how many 
> times each landcover class borders every other landcover class, like in 
> the example below.
>
>
>       Forest Grass Water
> Forest      0     0     0
> Grass       3     0     0
> Water       2     5     0
>
> I couldn't find a function that did this so I wrote a simple one using 
> some of the functions in the "network" library. I'm sure there is a way 
> to do this in the spdep package. How do I ask the nb object for the 
> neighbors of a specific node? The command in the network library is

An nb object is a list, so if you know the sequence number of the node of 
interest, say i in 1,...,n:

x <- nb[[i]]

will return an integer vector of neighbours, with no neighbours coded as 
a single zero (so drop zero).

If you need them coded by region.id, use attr(nb, "region.id") to extract 
these "row names", and match() the one you want against that vector to get 
i, reversing the process on x to get its "names".

However, for your end purpose, you might find that it is easier to go:

lw <- nb2listw(nb, style="B")
sn <- listw2sn(lw)

possibly with zero.policy=TRUE if needed, and then use sn$from and sn$to 
to index the category vector for tabulation (an sn object has a triplet 
for each link (from, to, weight)).

Note that double counting may be an issue for you using either method, as 
each symmetric link is counted twice, but with the categories reversed. 
You could try to remedy this by manipulating the input nb object, but your 
analysis of the table will show whether this is a problem for you.

Hope this helps,

Roger



>
> x <- get.neighborhood(network,node_id)
>
> I don't mind using the network library but it requires converting the nb 
> object to a network object and I'm sure there is a way to do this 
> without the conversion. Thanks for your help.
>
>
> Jonathan B. Thayn, Ph.D.
> Illinois State University
> Department of Geography - Geology
> 200A Felmley Hall
> Normal, Illinois 61790-4400
>
> (309) 438-8112
> jthayn at ilstu.edu
> my.ilstu.edu/~jthayn
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> 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