在保留节点ID的同时删除顶点 [英] delete vertices while preserving nodes IDs
问题描述
我正在使用删除顶点"功能,但是在我的网络上发现了一个奇怪的行为. 阅读igraph文档后,我发现:
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将从图形中移除指定的顶点及其相邻边.不保留这些顶点的id."
"delete.vertices removes the specified vertices from the graph together with their adjacent edges. The ids of the vertices are not preserved."
是否有任何变通方法来保留原始网络的ID?
is there any work-around to preserve the ids of the original network?
推荐答案
是的,为图分配一个顶点属性,也许name
属性是最好的.这些在删除后会保留.
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
如果要保留原始数字顶点ID,则将其分配为名称:
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
这篇关于在保留节点ID的同时删除顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!