1

I am using the function "delete vertices", and I found a strange behavior on my networks. After reading the documentation of igraph, I found that:

"delete.vertices removes the specified vertices from the graph together with their adjacent edges. The ids of the vertices are not preserved."

is there any work-around to preserve the ids of the original network?

M.M
  • 1,343
  • 7
  • 20
  • 49

1 Answers1

2

Yes, assign a vertex attribute to the graph, probably the name attribute is best. These are kept after deletion.

g <- graph.ring(10)
V(g)$name <- letters[1:10]
g2 <- delete.vertices(g, c("a", "b", "f"))
str(g2)
# IGRAPH UN-- 7 5 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
# + edges (vertex names):
# [1] c--d d--e g--h h--i i--j

If you want to preserve the original numeric vertex ids, then assign them as names:

gg <- graph.ring(10)
V(gg)$name <- V(gg)
gg2 <- delete.vertices(gg, c(1,2,6))
str(gg2)
# IGRAPH UN-- 7 5 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/n)
# + edges (vertex names):
# [1] 3-- 4 4-- 5 7-- 8 8-- 9 9--10
Gabor Csardi
  • 10,705
  • 1
  • 36
  • 53
  • Thanks for your answer. My original graph is an edge-list with the form: 1,2 2,10 1,15 .... I want to keep the numbers provided with that list i.e: 1,10,2,15 not a new naming or new numbering – M.M Nov 29 '13 at 09:34
  • Then assign these numbers as vertex names in the `name` attribute. – Gabor Csardi Nov 29 '13 at 14:51
  • Is it possible to assign the original node number as a name for the node. I care about the original ids of the graph. IOW, if I have an edge "1 -> 2", then the node "1" should have a name "1" and node "2" should have a name "2" – M.M Nov 30 '13 at 23:19