Search code examples
rgraphigraphedges

From igraph.es (edge sequence) to nodes in R


I want to set the edge attributes of a certain range of edges in a graph based on the values of the nodes they connect (in R igraph of course).

When I retrieve a certain edge in my graph object, I am served with an edge sequence object:

E(g)[1]
# + 1/2080 edge (vertex names):
# [1] 35->1
class(E(g)[1])
# [1] "igraph.es"

How can I get to the actual edges from that edge sequence? The only relevant function I have found is as_ids:

as_ids(E(g)[1])
# [1] "35|1"    

Then I have to split the string to get to the node ids, convert the ids to integers, fetch the nodes using the V(g)[x] notation, check the attributes I am interested in and finally set the edge attribute.

This is an impractical and wasteful process. Is there any more straightforward way to do the same?

I know the %--% notation and in certain cases it solves my issue by allowing me to filter the edges based on node attributes in advance. But in many other cases that notation doesn't help (when edge attribute values have a more complex relationship with node attributes), and I wonder if there is a more general way to get from one edge sequence to the corresponding pair of nodes.


Solution

  • You can use the ends function to get to the vertices:

    ends(g, E(g)[1])