The motivation for this problem is somewhat trivial, but I'm hoping it'll help me clear up some confusion I have about igraph and vertices. I apologize for the length but promise that it's a quick read.
I have a problem where it's very simple to recreate a graph from a picture with already labeled vertices using graph.formula(), and so naturally, unless I'm very careful about the order in which I enter vertices into graph.formula(), the vertex IDs and Name will not match.
Picture of graph for those who want it:
Code:
g<-graph.formula(1-2,1-4,1-6,3-5,3-5,3-6,3-7,4-5)
> get.edgelist(g)
[,1] [,2]
[1,] "1" "2"
[2,] "1" "4"
[3,] "1" "6"
[4,] "4" "5"
[5,] "6" "3"
[6,] "3" "5"
[7,] "3" "7"
> get.edgelist(g, names=FALSE)
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 3 6
[5,] 4 5
[6,] 5 6
[7,] 5 7
Now, for this graph, I'm interested in finding shortest paths and in using my own vertex names, so I type:
>get.shortest.paths(g,from="1",to="5")
[[1]]
[1] 1 3 6
But this output corresponds to the Vertex IDs and I'm interested in using the Vertex Names, so I try:
>V(g)$name [as.vector(get.shortest.paths(g,from="1",to="5"))]
Error in V(g)$name [as.vector(get.shortest.paths(g, from = "1", to = "5"))] :
invalid subscript type 'list'
and a bunch of variations of that, but I can't seem to get my list in terms of the Vertex Names.
So two questions:
1) How can I have it display the Vertex ID list that I get in terms of Vertex Names? I'm missing something obvious here...
2) Is there any way to re-assign my Vertex IDs? I haven't found any information on this so I'm thinking it's not possible, but it would be the simplest solution to my problem instead of working through attributes.
Thank you for any input!
permute.vertices
allows you to permute the vertices of the graph using a permutation vector. Theoretically, you could construct a permutation vector that maps your names into the correct IDs. – Tamás