1# Copyright 2001 by Roger Bivand 2# 3 4 5knn2nb <- function(knn, row.names=NULL, sym=FALSE) { 6 if (class(knn) != "knn") stop("Not a knn object") 7 res <- vector(mode="list", length=knn$np) 8 if (!is.null(row.names)) { 9 if(length(row.names) != knn$np) 10 stop("row.names wrong length") 11 if (length(unique(row.names)) != length(row.names)) 12 stop("non-unique row.names given") 13 } 14 if (knn$np < 1) stop("non-positive number of spatial units") 15 if (is.null(row.names)) row.names <- as.character(1:knn$np) 16 if(sym){ 17 to<-as.vector(knn$nn) 18 from<-rep(1:knn$np,knn$k) 19 for (i in 1:knn$np)res[[i]] <- sort(unique(c(to[from==i], 20 from[to==i]))) 21 } else { 22 for (i in 1:knn$np) res[[i]] <- sort(knn$nn[i,]) 23 } 24 attr(res, "region.id") <- row.names 25 attr(res, "call") <- attr(knn, "call") 26 attr(res, "sym") <- sym 27 attr(res, "type") <- "knn" 28 attr(res, "knn-k") <- knn$k 29 class(res) <- "nb" 30 res 31} 32