2

I'm new to R and igraph and I was wondering if anybody can help me with the following.

I want to find the edge weight between two vertices in a graph. My graph structure is defined by the normal ego (node1), alter (node2) and the weight of the edge between them.

I know that I can get the weight for each of the edges in the list of edges that originate from node number 5 using E(igraph_friendship) [ from(5) ]$weight And that I can find the weight for each of the edges in the list of edges that end onto node number 10 using E(igraph_friendship) [ to(10) ]$weight

But what if I simply want to find the weight of the edge that simple connects just node 5 and node 10?

Alternatively, if I can get the identifier of the edge that connects node 5 and 10 in the list of all edges, E(igraph_friendship), that would work too.

Thanks a lot for your help, I've been looking around a lot for it and I really appreciate your help!

newmathwhodis
  • 3,209
  • 2
  • 24
  • 26
  • Welcome to SO! If it's not too hard, please consider giving a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) as it will greatly increase your chances of getting an answer. – flodel Oct 19 '12 at 01:11

2 Answers2

0

This is actually quite easy in igraph 0.6 and above, because you can treat the graph as if it was an adjacency matrix (or weighed adjacency matrix in your case):

library(igraph)
g <- graph.ring(10)
g[1,2]
# [1] 1
E(g)$weight <- runif(ecount(g))
g[1,2]
# [1] 0.8115639

If you want to do this for the whole matrix, then you can simply do

g[]

Or, if you don't want sparse matrices, then

g[sparse=FALSE]

Please see ?"[.igraph" for more.

Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • This helped. However, it took me a while to figure out how to get my edge lists with weights into an adjancency matrix. I tried to do it the usual way using graph.data.frame but then would get a weird error when I tried translating the igraph object to and adjacency matrix (error: Error in .M.kind(x) : not yet implemented for matrix w/ typeof character). This post helped do the trick: https://sites.google.com/site/daishizuka/toolkits/sna/weighted-edgelists. What I found out from the email list help to work best was this simple operator directly on the igraph object: E(g)[5 %--% 10]$weight – newmathwhodis Oct 19 '12 at 18:41
  • You run into this bug I believe: https://bugs.launchpad.net/igraph/+bug/1025799 The workaround is to make your adjacency matrix dense if you can allow that, by setting `sparse=FALSE` in `get.adjacency()`. Or you can convert you character (=string) attribute to numeric (it is edge weight, right?) before calling `graph.data.frame()`. – Gabor Csardi Oct 19 '12 at 18:54
0

Gabor's use of the adjacency matrix helped. However, it took me a while to figure out how to get my edge lists with weights into an adjacency matrix. I tried to do it the usual way using graph.data.frame but then would get a weird error when I tried translating the igraph object to and adjacency matrix (error: Error in .M.kind(x) : not yet implemented for matrix w/ typeof character). This post helped do the trick: https://sites.google.com/site/daishizuka/toolkits/sna/weighted-edgelists.

However, what I found out from the R help email list help to work best was this simple operator directly on the igraph object: E(g)[5 %--% 10]$weight. See http://igraph.sourceforge.net/doc/R/iterators.html for details

newmathwhodis
  • 3,209
  • 2
  • 24
  • 26
  • If you want to do this for the whole matrix, then you can usually just do `g[]` and that gives you the (weighted) adjacency matrix. If you use sparse matrices (the default in igraph), then it only works if your weights are numeric. I have extended the example in my answer. – Gabor Csardi Oct 19 '12 at 18:59