这是一个tidygraph
+dplyr
选项
g %>%
activate(edges) %>%
filter(type == "a") %>%
as_tibble() %>%
group_by(to) %>%
summarise(indegree_a = n())
它给出了向内边缘的所有类型“A”的计数
# A tibble: 8 x 2
to indegree_a
<int> <int>
1 3 1
2 5 1
3 8 2
4 11 1
5 12 1
6 15 2
7 17 2
8 18 2
如果你想拥有所有节点的完整信息,你可以试试下面的代码
g %>%
activate(edges) %>%
as_tibble() %>%
select(-from) %>%
mutate(counts = 1) %>%
arrange(type) %>%
pivot_wider(
names_from = type,
values_from = counts,
values_fill = 0, values_fn = sum, names_glue = "indegree_{.name}"
) %>%
arrange(to)
这使
# A tibble: 18 x 4
to indegree_a indegree_b indegree_c
<int> <dbl> <dbl> <dbl>
1 1 0 1 2
2 2 0 0 1
3 3 1 0 0
4 4 0 2 0
5 5 1 0 2
6 6 0 1 1
7 8 2 1 0
8 9 0 0 1
9 10 0 0 1
10 11 1 1 0
11 12 1 0 1
12 13 0 1 2
13 15 2 0 0
14 16 0 3 0
15 17 2 0 0
16 18 2 1 0
17 19 0 0 1
18 20 0 1 0