Producing a conditional adjacency matrix
Thank you so much Roger! Works like a charm. Rhys
On Oct 1, 2019, at 3:38 PM, Roger Bivand <Roger.Bivand at nhh.no<mailto:Roger.Bivand at nhh.no>> wrote:
On Tue, 1 Oct 2019, Rhys Dubin wrote:
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. Leave col.gal.nb as it is, and generate a list of general weights: library(spdep) Loading required package: sp Loading required package: spData Loading required package: sf Linking to GEOS 3.7.2, GDAL 3.0.1, PROJ 6.2.0 data(columbus, package="spData") ls() [1] "bbs" "col.gal.nb" "columbus" "coords" "polys" col.gal.nb[[1]] [1] 2 3 set.seed(1) columbus$ID <- sample(1:3, nrow(columbus), replace=T) columbus$ID[1] [1] 1 columbus$ID[col.gal.nb[[1]]] [1] 3 1 # second entry equal glist <- lapply(seq(along=col.gal.nb), function(i) as.integer(columbus$ID[i] != columbus$ID[col.gal.nb[[i]]])) glist[[1]] [1] 1 0 # second weight zero