delete vertices while preserving nodes IDs

浪尽此生 提交于 2020-02-23 04:27:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!