[R-sig-Geo] Accessing the ids of neighbours in spdep

Roger Bivand Roger.Bivand at nhh.no
Mon Sep 29 11:53:58 CEST 2014


On Mon, 29 Sep 2014, Frede Aakmann Tøgersen wrote:

> Hi
>
> I'm not really sure I understand you but perhaps something like this:
>
> lapply(xx, paste, collapse = ", ")

Yes, this is the closest we can get without more input from Kenny.

This gives the 1-base integer indices in a list of single element 
character vectors. If the output should be a character vector with the 
same number of elements as the input:

lapply(xx, as.character)

will do it. But the IDs of the underlying entities (could be FIPS, NUTS3, 
whatever) are in attr(xx, "region.id"), so to get those:

lapply(xx, function(x) attr(xx, "region.id")[x])

is needed, or if a list of single element character vectors is required:

lapply(xx, function(x) paste(attr(xx, "region.id")[x], collapse = ", "))

Here the IDs are 0-based rather than 1-based, but the attr(xx, 
"region.id") values are arbitrary - they are taken from row.names() of the 
spatial object unless given as an argument to functions creating the "nb" 
object.

Hope this clarifies,

Roger

>
> Yours sincerely / Med venlig hilsen
>
>
> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling
>
> Technology & Service Solutions
> T +45 9730 5135
> M +45 2547 6050
> frtog at vestas.com
> http://www.vestas.com
>
> Company reg. name: Vestas Wind Systems A/S
> This e-mail is subject to our e-mail disclaimer statement.
> Please refer to www.vestas.com/legal/notice
> If you have received this e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-
>> project.org] On Behalf Of Kenny Bell
>> Sent: 29. september 2014 00:10
>> To: r-sig-geo at r-project.org
>> Subject: [R-sig-Geo] Accessing the ids of neighbours in spdep
>>
>> Hi,
>>
>> I am trying to get an actual list of neighbour ids out of an nb object. For
>> example, I have
>>
>> library(spdep)
>> example(columbus)
>> xx <- poly2nb(columbus)
>>
>> What would be my command to get the list of neighbours as a list of
>> character vectors?
>>
>> Thanks so much for any help,
>> Kenny
>>
>> 	[[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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no


More information about the R-sig-Geo mailing list