[R-sig-Geo] How to attach customized row.names to poly2nb()

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Thu Jan 18 13:52:37 CET 2024


Please never attach screenshots, nobody can copy/paste the code shown in them. Thus:

library(spdep)
library(spDataLarge)
data(bristol_zones)
bqueen <- poly2nb(bristol_zones, row.names=bristol_zones$name)

However, updating to also suppport sf removed the facility of setting through the row.names= argument:

attr(bqueen, "region.id")[bqueen[[1]]]
# [1] "2"  "39" "44" "50" "86" "94"

https://github.com/r-spatial/spdep/blob/b815397c2ce0d58b3b4ca0c36bba7b5c3abbbcc2/R/poly2nb.R#L16-L20

poly2nb(as(bristol_zones, "Spatial"), row.names = bristol_zones$name)
attr(bqueen, "region.id")[bqueen[[1]]]
# [1] "Bath and North East Somerset 003" "Bristol 038"
# [3] "Bristol 043"                      "Bristol 049"
# [5] "South Gloucestershire 024"        "South Gloucestershire 032"

I've raised this issue https://github.com/r-spatial/spdep/issues/143 so this should work again at the next release.

Roger
--
Roger Bivand
Emeritus Professor
Norwegian School of Economics
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway
Roger.Bivand using nhh.no

________________________________________
From: R-sig-Geo <r-sig-geo-bounces using r-project.org> on behalf of Xiang Ye via R-sig-Geo <r-sig-geo using r-project.org>
Sent: 18 January 2024 13:30
To: R Special Interest Group on using Geographical data and Mapping
Subject: [R-sig-Geo] How to attach customized row.names to poly2nb()

Dear community,

It should be a very simple question with a straightforward answer - so please forgive my foolishness.

By default, poly2nb() provides the neighbor list of a region in terms of numbers, for example:

library(spdep)
library(spDataLarge)

poly2nb(bristol_zones) -> bqueen
bqueen[1]

The output is
[cid:157ba602-300e-4f6b-8875-ea050def287c]
which is expected.

But often the case this is not convenient. So I want to provide a vector of names to label each region, and expect when I type bqueen[1], it can return something like a list of place names.

According to the help of poly2nb(), this is implemented by row.names(). However, it does not work:
[cid:be6a526c-5629-4c17-af44-972fb84f4a04]
It looks like the output neglects the argument of row.names.
How should I solve this issue?

Thanks in advance!


叶翔 YE, Xiang
THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
Ph.D. in Spatial Statistics



More information about the R-sig-Geo mailing list