[R-sig-Geo] spdep: show neighbors from "dnearneigh"

Kevin Ringelman kmringelman at ucdavis.edu
Tue Mar 22 18:43:54 CET 2011


Thanks Roger and Mathieu.  Instead of:
 [[1]]
  [1]   2  10  11  15  17

I now have:
1	2
1	10
1	11
1	15
1	17

For the last piece of this analysis, I would like to generate a list of
neighbor distances to complete my table.
nbd <- nbdist(nb, SpatialPoints)
nbd
[[1]]
 [1] 84.95881 90.35486 75.92760 22.20360 81.00617

But ideally, I would like to have
1	2	84.95881
1	10	90.35486
1	11	75.92760
1	15	22.20360
1	17	81.00617

I can't use nb2listw (and then merge), because this is an object of class
"nbdist".  I also cannot use the Mathieu's melt method, because I nb2mat
also requires an object of class "nb".

Suggestions?




-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: Tuesday, March 22, 2011 1:30 AM
To: Mathieu Rajerison
Cc: kmringelman at ucdavis.edu; r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] spdep: show neighbors from "dnearneigh"

On Tue, 22 Mar 2011, Mathieu Rajerison wrote:

> Hi,
>
> Maybe you should convert your nb object into a matrix using nb2mat then
use
> reshape package?
>
> library(reshape)
>
> df<-as.data.frame(nb.mat)
>
> df$id<-row.names(df)
>
> mdata<-melt(df,id="id")
>
> then subset your data for which mdata$value > 0

No, the question was how to convert to a two-column from-to 
representation, and a direct route is:

example(read.gal)
# to get an nb object
us48.q
us48.q[1:2]
res <- listw2sn(nb2listw(us48.q))[,1:2]
res[1:9,]
str(res)

If you need a matrix, do as.matrix(res).

Roger


>
>
> 2011/3/21 Kevin Ringelman <kmringelman at ucdavis.edu>
>
>> Thanks for your help Roger.  I have a couple more questions.
>>
>> print.default(nb) shows me what I'm looking for: for each region, I see
the
>> regions that are within my distance band.  For example:
>>
>> [[1]]
>>  [1]   2  10  11  15  17  18  19  32  40 554
>>
>> [[2]]
>>  [1]   1  15  17  21  33  34 426 511 554 557
>>
>> I want to make this information into a table, with the focal regions as
>> column 1, and the region IDs of it's neighbors as column 2 (similar to
>> neighbor analysis output in ArcGIS).  Continuing the example from above:
>> 1       2
>> 1       10
>> 1       11
>> 1       15
>> 1       17
>> (etc.)
>>
>> Even after I nuke the nb class
>> U <- unclass(nb)
>> I still can't access or manipulate the data...it doesn't appear as an
>> attribute
>> attribute(U)
>>
>> Any suggestions?
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
>> Sent: Saturday, March 05, 2011 10:03 AM
>> To: Kevin Ringelman
>> Cc: r-sig-geo at r-project.org
>> Subject: Re: [R-sig-Geo] spdep: show neighbors from "dnearneigh"
>>
>> On Fri, 4 Mar 2011, Kevin Ringelman wrote:
>>
>>> I am having trouble viewing the list of neighbors ("regions IDs") after
>>> created a nb object using the "dnearneigh" function in spdep.  I only
>> seem
>>> to get a summary (with # regions, # non-zero links, etc.).  This nb
>> object
>>> also doesn't take well to being converted to another type of object, or
>>> exported from R.  How I view the list of neighbors?
>>>
>>
>> In S and R, objects with a class attribute, such as "nb" objects, may
have
>> display methods specific to the class. If you just say:
>>
>>> nb
>>
>> then this is expanded internally to print(nb), and since nb is an object
>> of class "nb", the print.nb() method is chosen. If you want the default
>> print method, call it as print.default(nb). Conversion of objects of one
>> class to another class may be done by coercion where coercion methods are
>> provided. No such methods are available for nb objects. There are
>> functions to do things like this, but not methods. For example, to make
an
>> nb object into a row-standardised matrix, you might do:
>>
>>> Wmat <- nb2mat(nb, style="W")
>>
>> but you should avoid this if your number of observations is large. To
make
>> a sparse matrix, several steps are required:
>>
>>> lw <- nb2listw(nb, style="W")
>>> spWmat <- as(as_dgRMatrix_listw(lw), "CsparseMatrix")
>>
>> using the nb2listw() and (ugly name) as_dgRMatrix_listw() functions, and
>> coercion from one representation to another using new-style classes
>> defined in the Matrix package.
>>
>>>
>>>
>>> Some additional background: I'm identifying all neighboring bird nests
>>> within 100m of each nest.  For this particular analysis, I ultimately
>> want
>>> to calculate the average vegetation height of neighboring nests to
>> compare
>>> with the focal nest.  This will involve generating a list of neighbors,
>> and
>>> merging that list with my other data.
>>>
>>
>> Note that an nb object is a list - to nuke the class, do:
>>
>>> class(nb) <- NULL
>>
>> which lets you call print.default(), but to get at the attribute you
want,
>> just do:
>>
>>> attr(nb, "region.id")
>>
>> to call print.default on the character vector it contains.
>>
>> Hope this clarifies,
>>
>> Roger
>>
>>>
>>>
>>> Thanks,
>>>
>>> Kevin
>>>
>>>
>>>       [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> 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
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> 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