Skip to content
Prev 17224 / 29559 Next

Class nb (spdep) from class igraph

Thanks Roger.

Apologies: the two functions I intended to reference in my original post 
were igraph::get.adjacency() and igraph::get.edgelist() which produce a 
slightly clearer broth (I like this expression "fish soup").

Your suggestion requiring a third party package seems the easiest. I 
found another 3rd party approach using the ade4 package which has two 
functions ade4::neig() and ade4::neig2nb() that together do the trick.  
(Note this approach ignores edge weights).

library(igraph)
library(spdep)

## igraph2nb
## Convert igraph undirected graphs (of S3 class igraph) into nb objects 
(class spdep)
## No checking performed
igraph2nb <- function(gr) {
     library(ade4)
     return(neig2nb(neig(edges=get.edgelist(gr))))
}

## Create simple igraph object
ringGraph <- graph.ring(10)

## Convert to nb object
ringGraph_nb <- igraph2nb(ringGraph)

## Visualize using plot.nb
plot(ringGraph_nb, coords=cbind(runif(10), runif(10)))


Best,
Paul
On 22/01/2013 1:44 AM, Roger Bivand wrote: