在 tidygraph 中过滤

ℕʘʘ*_*ḆḽḘ 6 r igraph tidygraph

考虑这个简单的例子

library(tidygraph)

mynodes <- tibble(id = c(1,2,3,4,5))

myedges <- tibble(from = c(1,1,1,5),
                  to = c(1,2,3,4),
                  power = c(10,10,10,3))

tbl_graph(nodes = mynodes, edges = myedges)
# A tbl_graph: 5 nodes and 4 edges
#
# A directed multigraph with 2 components
#
# Node Data: 5 x 1 (active)
     id
  <dbl>
1     1
2     2
3     3
4     4
5     5
#
# Edge Data: 4 x 3
   from    to power
  <int> <int> <dbl>
1     1     1    10
2     1     2    10
3     1     3    10
# ? with 1 more row
Run Code Online (Sandbox Code Playgroud)

我知道我可以用来filter轻松过滤节点或边缘。

我的问题是如何根据边缘的条件过滤节点。

例如运行:

mygraph %>% activate(edges) %>% filter(power == 3)

仍然会返回所有节点,这在绘图时很烦人(这些节点将没有边缘!)。

如何保留与过滤后的边集连接的所有节点?

# A tbl_graph: 5 nodes and 1 edges
#
# A rooted forest with 4 trees
#
# Edge Data: 1 x 3 (active)
   from    to power
  <int> <int> <dbl>
1     5     4     3
#
# Node Data: 5 x 1
     id
  <dbl>
1     1
2     2
3     3
# ? with 2 more rows
Run Code Online (Sandbox Code Playgroud)

ℕʘʘ*_*ḆḽḘ 2

实际上有一个非常简单的解决方案

activate(nodes) %>% 
  filter(!node_is_isolated())
Run Code Online (Sandbox Code Playgroud)

  • 所以,它是:`mygraph %&gt;% activate(edges) %&gt;% filter(power == 3) %&gt;% activate(nodes) %&gt;% filter(!node_is_isolated())` (3认同)