问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/20245234/delete-vertices-while-preserving-nodes-ids