# [R-sig-Geo] Producing a conditional adjacency matrix

Rhys Dubin rhy@@dub|n @end|ng |rom @@nt@ox@@c@uk
Tue Oct 1 14:12:54 CEST 2019

```Hi Roger,

Including a bit more detail below (my apologies if this is just complete gibberish — first time posting to a forum like this):

First, I add a random ID variable to the columbus dataset indicating the variable I’d like to base my exclusions on in the final neighbor list:
> columbus\$ID <- sample(1:3, nrow(columbus), replace=T)

In my case, that produced the following IDs for the first 4 polyids:

POLYID 1: 3
POLYID 2: 2
POLYID 3: 3
POLYID 4: 1

Second, col.ga.nb indicates that each of the previous POLYID’s has the following neighbors

> View(col.gal.nb)

POLYID 1 (w new assigned ID of 3): 2, 3
POLYID 2 (“ “ 2): 1, 3, 4
POLYID 3 (“ “ 3): 1, 2, 4, 5
POLYID 4 (“ “ 1): 2, 3, 5, 8

Third (or brevity’s sake) looking only at POLYID 1 (with new ID = 3):

Its neighbors from col.ga.nb (POLYID 2 and 3) have ID values of 3 and 1 respectively

My end goal would be to produce a new list in which POLYID 1’s only neighbor is POLYID 3 (with an ID of 1). POLYID 2 would be excluded, as its ID value matches its parent.

The same would be true of each subsequent polygon.

In essence, the same format you mentioned at the end of your reply: a glist including all neighbors j with a value of x not equal to x[i], and excluding all with x[j] == x[i].

Best,

Rhys

> On Oct 1, 2019, at 1:22 PM, Roger Bivand <Roger.Bivand using nhh.no> wrote:
>
> On Mon, 30 Sep 2019, Rhys Dubin wrote:
>
>> Hi all,
>>
>> I have a question regarding matrices (produced using poy2nb in spdep).
>>
>> I’m trying to produce an adjacency matrix from a GIS Polygon shapefile with about 500 individual polygons (queen or rook — it doesn’t really matter for now), but conditional on the value of the parent polygon.
>>
>> For instance, if a polygon had an arbitrary value of 1, I want to return a list of all neighboring polygons that have a value of 2 or 3 (ie. ≠ 1). The same would go for a polygon with a value of 2 (ie. return all neighbors with a value of ≠ 2).
>>
>> Is there a solution to this issue built into spdep, do I need to use a different package, or is it possible to accomplish this with a subsetting command?
>
> No built-in solution, but to make progress a clear, reproducible example is essential, otherwise we're guessing what you want. For example, add a doctored variable to the eire or columbus data sets to show your needs. Do you want a glist including (1) all neighbours j with a value of x not equal to x[i], and excluding (0) all with x[j] == x[i]?
>
> Roger
>
>>
>> Thanks!
>>
>> Rhys
>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo using 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; e-mail: Roger.Bivand using nhh.no
> https://orcid.org/0000-0003-2392-6140