如果您从 R 中使用 igraph,请使用此选项
igraph-es-indexing {igraph} | R 文档 |
边序列的索引方式非常类似于普通的 R 数值向量,但有一些额外的功能。
## S3 method for class 'igraph.es'
x[...]
x |
一个边序列 |
... |
索引,详见下文。 |
另一个边序列,指向同一个图。
当在括号中使用多个索引时,它们都会被独立评估,然后使用 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()
, igraph-es-attributes
, igraph-es-indexing2
, igraph-vs-attributes
, igraph-vs-indexing2
, igraph-vs-indexing
, print.igraph.es()
, print.igraph.vs()
其他顶点和边序列操作:c.igraph.es()
, c.igraph.vs()
, difference.igraph.es()
, difference.igraph.vs()
, igraph-es-indexing2
, igraph-vs-indexing2
, igraph-vs-indexing
, 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 ]
E(g) [ 1:5 %->% 1:6 ]
E(g) [ 1:3 %<-% 2:6 ]
# -----------------------------------------------------------------
# The edges along the diameter
g <- sample_pa(100, directed = FALSE)
d <- get_diameter(g)
E(g, path = d)
# -----------------------------------------------------------------
# Select edges based on attributes
g <- sample_gnp(20, 3/20) %>%
set_edge_attr("weight", value = rnorm(gsize(.)))
E(g)[[ weight < 0 ]]
# 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]