如何将 tidygraph tibble 的节点和边缘部分分离为两个单独的纯 tibble
How to detach nodes and edge part of a tidygraph tibble into two separate pure tibbles
我有以下 tidygraph tibble:
rstat_nodes <- data.frame(name = c("Hadley", "David", "Romain", "Julia"), sex = c("M","M","M","F"))
rstat_edges <- data.frame(from = c(1, 1, 1, 2, 3, 3, 4, 4, 4),
to = c(2, 3, 4, 1, 1, 2, 1, 2, 3))
g <- tbl_graph(nodes = rstat_nodes, edges = rstat_edges)
g
看起来像这样:
> g
# A tbl_graph: 4 nodes and 9 edges
#
# A directed simple graph with 1 component
#
# Node Data: 4 x 2 (active)
name sex
<fct> <fct>
1 Hadley M
2 David M
3 Romain M
4 Julia F
#
# Edge Data: 9 x 2
from to
<int> <int>
1 1 2
2 1 3
3 1 4
# … with 6 more rows
有没有方便的方法将它们分成 two pure tibbles 一个用于
节点部分另一个边缘部分。
我对 tidygraphs 比较陌生。很想看看数据结构是什么样的,但也许这就是您要找的?
# to get nodes
g %>% activate(nodes) %>% as_tibble()
# to get edges
g %>% activate(nodes) %>% as_tibble()
我有以下 tidygraph tibble:
rstat_nodes <- data.frame(name = c("Hadley", "David", "Romain", "Julia"), sex = c("M","M","M","F"))
rstat_edges <- data.frame(from = c(1, 1, 1, 2, 3, 3, 4, 4, 4),
to = c(2, 3, 4, 1, 1, 2, 1, 2, 3))
g <- tbl_graph(nodes = rstat_nodes, edges = rstat_edges)
g
看起来像这样:
> g
# A tbl_graph: 4 nodes and 9 edges
#
# A directed simple graph with 1 component
#
# Node Data: 4 x 2 (active)
name sex
<fct> <fct>
1 Hadley M
2 David M
3 Romain M
4 Julia F
#
# Edge Data: 9 x 2
from to
<int> <int>
1 1 2
2 1 3
3 1 4
# … with 6 more rows
有没有方便的方法将它们分成 two pure tibbles 一个用于 节点部分另一个边缘部分。
我对 tidygraphs 比较陌生。很想看看数据结构是什么样的,但也许这就是您要找的?
# to get nodes
g %>% activate(nodes) %>% as_tibble()
# to get edges
g %>% activate(nodes) %>% as_tibble()