Search code examples
rattributesigraphvertexedge-list

How to access vertices via edge list iteration igraph R


I am trying to access the vertices of each vertex pair in an edge using a for loop like this:

for (e in E(G)) { do stuff }

However, I don't know how to get each vertex in e. I am trying to return the type of each vertex, which is in the vertex list. I can get the vertex attribute like I do below, though I am not sure if this is an ok way to iterate through the edge list and get all the edges:

i = 1

for (e in get.edgelist(G)) {
    if(V(G)[get.edgelist(G)[i,][1]]$type %in% vector &&
       V(G)[get.edgelist(G)[i,][2]]$type %in% vector ) { do stuff }
    i = i + 1
}

I also noticed that e in E(G) returns an index, so is it correct to do it like this?

for (e in E(G)) {
    if(V(G)[get.edgelist(G)[e,][1]]$type %in% vector &&
       V(G)[get.edgelist(G)[e,][2]]$type %in% vector ) { do stuff }
}

I believe the last example does what I want, but I am not sure of the mechanics of iGraph to be sure I am indexing the edges correctly. Any advice would be greatly appreciated. Thanks in advance.


Solution

  • Summing up edge frequencies this appears to work:

    for (e in E(G)) {
     if(V(G)[get.edgelist(G)[e,][1]]$type %in% vector &&
      V(G)[get.edgelist(G)[e,][2]]$type %in% vector ) { do stuff }
    }