从使用 igraph 构建的图形中,我尝试使用 igraph::E()[] 和 igraph::edge_attr() 按边缘属性选择边缘。在选择条件中稍微更改我的代码,应用选择会得到不同的结果。我觉得这出乎意料。我无法解释。请告诉我原因。
library("igraph")
# create dummy graph and selection attribute
set.seed(42)
g <- make_ring(10)
g <- set_edge_attr(g, "group", value = sample(c("A", "B"), 10, replace = TRUE))
edge_attr(g)
# select by
edge_attr_group <- "group"
random_name_of_variable <- "A"
group <- "A"
首先,认识到这一点
# indices to select
which(edge_attr(g, edge_attr_group) == "A")
which(edge_attr(g, edge_attr_group) == random_name_of_variable)
which(edge_attr(g, edge_attr_group) == group)
# all equal and expected
到目前为止,我认为一切顺利。但然后应用选择条件
# apply selection
E(g)[which(edge_attr(g, edge_attr_group) == "A")]
E(g)[which(edge_attr(g, edge_attr_group) == random_name_of_variable)]
# both equal and expected, but
E(g)[which(edge_attr(g, edge_attr_group) == group)]
# is not equal and thus unexpected
因此,选择使用静态值(“A”)或条件变量的随机名称(random_name_of_variable),我们就得到了预期的结果。但是当选择使用 group 变量时,我们不会得到相同的结果。这是什么原因呢?我认识到变量名称与属性名称相同,但为什么这很重要?
供引用, session 信息和我的输出:
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] igraph_1.3.2
loaded via a namespace (and not attached):
[1] compiler_4.1.2 magrittr_2.0.3 tools_4.1.2 pkgconfig_2.0.3
> which(edge_attr(g, edge_attr_group) == "A")
[1] 1 2 3 4 9
> which(edge_attr(g, edge_attr_group) == random_name_of_variable)
[1] 1 2 3 4 9
> which(edge_attr(g, edge_attr_group) == group)
[1] 1 2 3 4 9
> E(g)[which(edge_attr(g, edge_attr_group) == "A")]
+ 5/10 edges from 0e5f311:
[1] 1-- 2 2-- 3 3-- 4 4-- 5 9--10
> E(g)[which(edge_attr(g, edge_attr_group) == random_name_of_variable)]
+ 5/10 edges from 0e5f311:
[1] 1-- 2 2-- 3 3-- 4 4-- 5 9--10
> E(g)[which(edge_attr(g, edge_attr_group) == group)]
+ 10/10 edges from 0e5f311:
[1] 1-- 2 2-- 3 3-- 4 4-- 5 5-- 6 6-- 7 7-- 8 8-- 9 9--10 1--10
最佳答案
这是因为在索引到边或顶点序列时,属性名称会被特殊解释。文档here对此进行了描述。和 here :
When indexing edge sequences, edge attributes can be referred to simply by using their names. E.g. if a graph has a weight edge attribute, then
E(G)[weight > 1]
selects all edges with a larger than one weight. See more examples below.
因此,从组“A”
中选择边的更简单方法是E(g)[group ==“A”]
。
更新:自 R/igraph 1.3.3 起,可以使用 .data
和 .env
代词消除名称歧义。
尝试将非限定名称解释为属性。如果该名称的属性不存在,则该名称将被视为变量。
因此,这选择了第三条边:
> g <- make_ring(5)
> foo <- 3
> E(g)[foo]
+ 1/5 edge from febda41:
[1] 3--4
但是一旦我们设置了名为 foo
的属性,它的行为就会发生变化:
> E(g)$foo <- LETTERS[1:5]
> E(g)[foo]
Error in simple_es_index(x, ii) : Unknown edge selected
要指定 foo
是一个变量,并避免任何歧义,我们可以将其称为 .env$foo
:
> E(g)[.env$foo]
+ 1/5 edge from febda41:
[1] 3--4
为了完整起见,我们还可以明确表示它引用属性名称:
> E(g)[.data$foo == 'D']
+ 1/5 edge from febda41:
[1] 4--5
关于r - 使用 igraph::E()[] 和 igraph::edge_attr() 按边缘属性选择边缘时,我得到不同的(意外的)结果,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72688526/