边序列可以像普通的数值R向量一样被索引,但有一些额外的功能。
多重索引
当在方括号中使用多个索引时,它们都会被独立评估,然后使用c()
函数将结果连接起来。例如,E(g)[1, 2, .inc(1)]
相当于c(E(g)[1], E(g)[2], E(g)[.inc(1)])
。
索引类型
边序列可以用正数值向量、负数值向量、逻辑向量、字符向量索引
当用正数值向量索引时,会选择序列中给定位置的边。这与用正数值向量索引常规的R原子向量相同。
当用负数值向量索引时,会省略序列中给定位置的边。同样,这与索引常规的R原子向量相同。
当用逻辑向量索引时,边序列和索引的长度必须匹配,并且会选择索引为
TRUE
的边。命名的图可以用字符向量索引,以选择具有给定名称的边。请注意,一个图可能同时具有边名称和顶点名称,并且两者都可以用于选择边。边名称只是用作数值边ID向量的名称。顶点名称实际上只在没有多重边的图中使用,并且必须用
|
竖线字符分隔,以选择与两个给定顶点关联的边。请参见下面的示例。
边属性
在索引边序列时,可以通过简单地使用它们的名称来引用边属性。例如,如果一个图具有weight
边属性,那么E(G)[weight > 1]
会选择所有权重大于1的边。请参见下面的更多示例。请注意,属性名称会屏蔽调用环境中存在的变量的名称;如果您需要查找一个变量,并且不希望类似命名的边属性屏蔽它,请使用.env
代词在调用环境中执行名称查找。换句话说,使用E(g)[.env$weight > 1]
以确保即使存在具有相同名称的边属性,weight
也是从调用环境中查找的。类似地,您可以使用.data
仅匹配属性名称。
特殊函数
有一些特殊的igraph函数只能在索引边序列的表达式中使用
.inc
接受一个顶点序列,并选择所有至少有一个关联顶点在顶点序列中的边。
.from
类似于
.inc()
,但只考虑边的尾部。.to
类似于
.inc()
,但只考虑边的头部。\%--\%
一个特殊的运算符,可用于选择两个顶点集之间的所有边。它忽略有向图中的边方向。
\%->\%
类似于
\%--\%
,但在有向图中,选择从左侧参数指向右侧参数的边。\%<-\%
类似于
\%--\%
,但在有向图中,选择到左侧参数,从右侧参数指向的边。
请注意,可以一起使用多个特殊函数,或者与常规索引一起使用,然后将它们的结果连接起来。请参见下面的更多示例。
参见
其他顶点和边序列:E()
、V()
、as_ids()
、igraph-es-attributes
、igraph-es-indexing2
、igraph-vs-attributes
、igraph-vs-indexing
、igraph-vs-indexing2
、print.igraph.es()
、print.igraph.vs()
其他顶点和边序列操作:c.igraph.es()
、c.igraph.vs()
、difference.igraph.es()
、difference.igraph.vs()
、igraph-es-indexing2
、igraph-vs-indexing
、igraph-vs-indexing2
、intersection.igraph.es()
、intersection.igraph.vs()
、rev.igraph.es()
、rev.igraph.vs()
、union.igraph.es()
、union.igraph.vs()
、unique.igraph.es()
、unique.igraph.vs()
示例
# -----------------------------------------------------------------
# Special operators for indexing based on graph structure
g <- sample_pa(100, power = 0.3)
E(g)[1:3 %--% 2:6]
#> + 4/99 edges from c986285:
#> [1] 2->1 3->2 4->1 5->3
E(g)[1:5 %->% 1:6]
#> + 4/99 edges from c986285:
#> [1] 2->1 3->2 4->1 5->3
E(g)[1:3 %<-% 2:6]
#> + 4/99 edges from c986285:
#> [1] 2->1 3->2 4->1 5->3
# -----------------------------------------------------------------
# The edges along the diameter
g <- sample_pa(100, directed = FALSE)
d <- get_diameter(g)
E(g, path = d)
#> + 11/99 edges from 9d0ee7f:
#> [1] 56--77 49--56 25--49 24--25 2--24 2-- 4 4--10 10--16 16--22 22--67
#> [11] 67--92
# -----------------------------------------------------------------
# Select edges based on attributes
g <- sample_gnp(20, 3 / 20) %>%
set_edge_attr("weight", value = rnorm(gsize(.)))
E(g)[[weight < 0]]
#> + 11/19 edges from 4af1e5e:
#> tail head tid hid weight
#> 1 1 2 1 2 -1.3803023
#> 2 5 6 5 6 -0.7457159
#> 5 2 10 2 10 -0.1886325
#> 7 7 14 7 14 -1.1246830
#> 9 11 14 11 14 -0.6231949
#> 10 13 15 13 15 -1.0164530
#> 12 3 17 3 17 -0.5737814
#> 13 4 19 4 19 -1.2981434
#> 14 8 19 8 19 -0.1116867
#> 15 12 19 12 19 -0.5027056
#> 16 18 19 18 19 -0.8653440
# Indexing with a variable whose name matches the name of an attribute
# may fail; use .env to force the name lookup in the parent environment
E(g)$x <- E(g)$weight
x <- 2
E(g)[.env$x]
#> + 1/19 edge from 4af1e5e:
#> [1] 5--6