从使用igraph构建的图中,我try 使用igraph::E()[]和igraph::edge\u attr()按边属性 Select 边.在 Select 条件中稍微更改代码,应用 Select 会得到不同的结果.我觉得这出乎意料.我无法解释.请告诉我原因.
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
我想目前为止还不错.然后应用 Select 条件
# 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)进行 Select ,我们可以得到预期的结果.但当使用group变量进行 Select 时,我们不会得到相同的结果.这是什么原因?我知道变量名与属性名相同,但这又有什么关系呢?
作为参考,会话信息和我的输出:
> 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