[R-sig-Geo] Class nb (spdep) from class igraph

Roger Bivand Roger.Bivand at nhh.no
Tue Jan 22 08:44:45 CET 2013


On Mon, 21 Jan 2013, Paul Galpern wrote:

> Is there a simple way to produce an nb object (in spdep) from a weighted or 
> unweighted undirected graph (in igraph).

No, unless you can make them back into a (possibly sparse) adjacency 
matrix. The igraph S3 class (returned by graph.adjacency) is fish soup, 
while the input adjacency matrix would work directly with 
spdep::mat2listw().

Please provide sample code to illustrate your question - preferably 
showing how to go from and to nb objects. If you add igraph <-> graphNEL 
<-> sparse matrix, you can get there, but not igraph <-> nb in one hop, 
going through graphNEL is needed:

library(igraph)
set.seed(1)
adjm <- matrix(sample(0:1, 100, replace=TRUE, prob=c(0.8,0.2)), nc=10)
g1 <- graph.adjacency(adjm)
library(graph)
g1G <- igraph.to.graphNEL(g1)
library(Matrix)
sM <- as(g1G, "sparseMatrix")
library(spdep)
nb <- mat2listw(sM)$neighbours
nb
sum(adjm)

If you wanted to contribute a function, the igraph.[to|from].graphNEL code 
would probably show how it could be done, because graphNEL are quite 
similar to listw objects.

Hope this helps,

Roger

>
> Could the output of igraph::graph.adjacency(x) be converted to class nb?
>
> Alternatively, igraph::graph.edgelist(x) could be manipulated into class 
> Graph, and then spdep::graph2nb() used.
>
> Am I missing an obvious way to do this?
>
> Thanks,
> Paul
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list