[R-sig-Geo] How can I view a list of neighbors from the outcome knn2nb

Takatsugu Kobayashi tkobayas at indiana.edu
Sun Mar 4 19:41:54 CET 2007


Hi

I am trying to create a list of nearest neighbors using knearneigh and 
dinearneigh. To understand these I have been coding what these functions do:

For example, for dnearneigh, suppose I have x-y coordinates and the 
corresponding z values

# calculate distances between observations
data<-cbind(x,y,z)
coords<-cbind(x,y)
dis<-as.matrix(dist(coords,diag=T,upper=T))

# Create distance threshold =2
thrs<-2

nb<-list(vector("numeric",length(z)))

for (j in 1:nrow(data)){
    nb[[j]]<-as.vector(which(dis[j,]<=thrs))
}

nb

Then I can use this nb list to compute many local statistics.

For knearneigh, I can sort the distance matrix in ascending order and 
take the first n numbers as nearest neighbors. Then, how can I take a 
look at a list of neighbors when I use knearneigh?  unlist(nb,coords) 
just breaks the [[ ]] structure...

Thank for help in advance.

Taka




More information about the R-sig-Geo mailing list