From a graph built with igraph, I'm trying to select edges by edge attribute, using igraph::E()[] and igraph::edge_attr(). Changing my code slightly in the selection condition, I get different results applying the selection. I find this unexpected. I can't explain it. Please let me know why.
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"
First, recognize that
# 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
So far so good I guess. But then applying the selection condition
# 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
So, selecting using a static value ("A") or a random name for the condition variable (random_name_of_variable), we get the intended results. But when selecting using the group variable, we don't get the same result. What is the reason for this? I recognize that the variable name is the same as the attribute name, but why would that matter?
For reference, session info and my output:
> 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