user8003788
user8003788

Reputation: 253

read a graph by vertices not as an edge list in R

To explain: I have an undirected graph stored in a text file as edges where each line consist of two values represent an edge, like:

5 10
1000 2
212 420
  .
  . 
  .

Normally when reading a graph in R from a file (using igraph), it will be read as edges so to call the edges of the graph "g" we write E(g) and to call the vertices of "g" we write V(g) and to call both vertices of a certain edge (i.e to call a certain edge (edge i)) we write E(g)[i].

My question: Is there a similar way to call one vertex only inside an edge not to call both of them.

For example, if I need the second vertex in the third edge then what I need to type? Also from the beginning, is there something on igraph to read the graph as vertices and not as edges? like to read the graph as a table with two columns such that each edge to be read as X[i][1], X[i][2].

I need this because I want to do a loop among all vertices and to choose them separately from the edge and I think it is possible if each vertex was labeled like an element in a table.

Many thanks in advance for any help

Upvotes: 1

Views: 344

Answers (1)

d.b
d.b

Reputation: 32558

If you have a two column table with vertices, you could use graph_from_data_frame to convert it into graph. To get nodes on particular edge, you can use ends.

#DATA
set.seed(2)
m = cbind(FROM = sample(LETTERS[1:5], 10, TRUE), TO = sample(LETTERS[6:10], 10, TRUE))

#Convert to graph
g = graph_from_data_frame(m, directed = FALSE)
#plot(g)

#Second vertex on third edge
ends(graph = g, es = 3)[2]
#[1] "I"

Upvotes: 1

Related Questions