[R] R Sig-Geo group - loop for creating spatial matrix

Giorgio Garziano giorgio.garziano at ericsson.com
Tue Feb 2 09:48:37 CET 2016

Dear Francesco,

from that point ahead, you have to handle a list of “nb” objects.
I used class() in order to show you what the list is made of.

The R code outlined in your reply may follow a pattern like this (showing an option):

for (val in z) {
    dlwknn.B <- nb2listw(neighbors.knn[[val]], style="B", zero.policy=TRUE)
    globalG.test(CRIME, dlwknn.B, zero.policy=F)

You may want to collect the results of the GlobalG.test in a list as well, the

help(globalG.test) provides an example at the bottom of its R help page.

To further point out that R language makes possible to implement functions call
over a list without specifying “for” loops. Some examples at:


The R-SIG-Geo mailing list is reachable at:


if that was your original intention.



From: Francesco Perugini [mailto:francesco.perugini at yahoo.it]
Sent: martedì 2 febbraio 2016 09:24
To: Giorgio Garziano
Subject: Re: [R] R Sig-Geo group - loop for creating spatial matrix

Dear Giorgio,
thanks a lot for your reply.
From here now,  I want to implement the Global G test for spatial autocorrelation for the generated different matrix and plot the Global G statistic (on the y-axes) against different val (on the x-axis). How should the code be? I've tried the following:

# Global G
dlwknn.B <- nb2listw(class(neighbors.knn[[val]]), style="B", zero.policy=TRUE)
globalG.test(CRIME, dlwknn.B, zero.policy=F)

but it is not working. Thanks a lot for your help..

Da: Giorgio Garziano <giorgio.garziano at ericsson.com<mailto:giorgio.garziano at ericsson.com>>
A: "r-help at r-project.org<mailto:r-help at r-project.org>" <r-help at r-project.org<mailto:r-help at r-project.org>>
Cc: "francesco.perugini at yahoo.it<mailto:francesco.perugini at yahoo.it>" <francesco.perugini at yahoo.it<mailto:francesco.perugini at yahoo.it>>
Inviato: Lunedì 1 Febbraio 2016 20:39
Oggetto: Re: [R] R Sig-Geo group - loop for creating spatial matrix

You may handle that as a list of “nb” objects.

coord <- coordinates(columbus)

z <- c(1,2,3,4,5,6,7,8,9)
neighbors.knn <- list()

for (val in z) {
  neighbors.knn <- c(neighbors.knn, list(knn2nb(knearneigh(coord, val, longlat=F), sym=F)))


plot(neighbors.knn[[1]], coord)

plot(neighbors.knn[[2]], coord)

and so on.



	[[alternative HTML version deleted]]

More information about the R-help mailing list