[英]Delete edges between vertices in igraph R
I have an unweighted and undirected graph (A) with 10 vertices and 10 edges.我有一个具有 10 个顶点和 10 个边的未加权和无向图 (A)。
> A
IGRAPH UNW- 10 10 --
+ attr: name (v/c), weight (e/n)
I want to delete a bunch of edges from this graph as defined by vertex pairs, so for example, I want to delete the following edges:我想从该图中删除由顶点对定义的一堆边,例如,我想删除以下边:
V4 -- V5
V3 -- V7
V3 -- V6
These edges are stored in a data frame called "edges".这些边缘存储在称为“边缘”的数据框中。 I want to delete these edges in one go.
我想在一个 go 中删除这些边缘。 I tried:
我试过了:
> delete.edges(A,t(edges))
but this does not work and returns the error:但这不起作用并返回错误:
Error in as.igraph.es(graph, edges) : Invalid edge names
In addition: Warning message:
In as.igraph.es(graph, edges) : NAs introduced by coercion
Why does this not work when the equivalent command for adding edges does work?当添加边的等效命令有效时,为什么这不起作用?
add.edges(A,t(edges))
add.edges(A,t(边))
How can I delete these edges from graph A in one command?如何在一个命令中从图 A 中删除这些边? Thanks
谢谢
Probably the simplest way is to use the graph as an adjacency matrix: 可能最简单的方法是使用图形作为邻接矩阵:
library(igraph)
g <- graph.ring(10)
V(g)$name <- letters[1:10]
str(g)
# IGRAPH UN-- 10 10 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
# + edges (vertex names):
# [1] a--b b--c c--d d--e e--f f--g g--h h--i i--j a--j
g[ from=c("a","b","c"), to=c("b","c","d") ] <- 0
str(g)
# IGRAPH UN-- 10 7 -- Ring graph
# + attr: name (g/c), mutual (g/l), circular (g/l), name (v/c)
# + edges (vertex names):
# [1] d--e e--f f--g g--h h--i i--j a--j
See more at http://igraph.org/r/doc/graph.structure.html . 有关更多信息,请访问http://igraph.org/r/doc/graph.structure.html 。
The manual suggests to either use E
to extract the edges you want to remove from the graph, or edges
to construct them from their names (we do not know what your edges
data.frame contains). 手册建议使用
E
来提取要从图形中删除的edges
,或使用edges
从名称中构造它们(我们不知道你的edges
data.frame包含什么)。
library(igraph)
# Sample graph
g <- graph.ring(10)
plot(g)
# Edges to remove, as a data.frame
e <- data.frame(
from = 1:3,
to = 2:4
)
# Convert the data.frame to edges
e <- apply(e, 1, paste, collapse="|")
e <- edges(e)
# Remove the edges and plot the resulting graph.
plot( g - e )
Delete_edges takes a series of edge IDs. Delete_edges 采用一系列边 ID。 Deletion of edges via their incident vertices requires an additional conversion to edge IDs.
通过其入射顶点删除边需要额外转换为边 ID。
ei <- get.edge.ids(g, c(3,6, 3,7, 4,5))
ei <- get.edge.ids(g, c(3,6, 3,7, 4,5))
g <- graph(c(3,6, 3,7, 4,5), 10, directed=FALSE) +
path(1,2,5,6,7,8,9,10)
ei <- get.edge.ids(g, c(3,6, 3,7, 4,5))
g2 <- delete_edges(g, ei)
Edges can also be removed by symbolic names.边缘也可以通过符号名称删除。
g3 <- set_vertex_attr( g
, name="name"
, value=strsplit(paste0("V", seq_len(10)), " " )
)
g4 <- delete_edges( g3
, get.edge.ids(g3, c("V3","V6", "V3","V7", "V4","V5"))
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.