rigraph - 识别节点与子图的关系,而不管与所述子图的从属关系

r igraph - Identify ties of nodes to a subgraph regardless of affiliation to said subgraph

如何计算节点与同一图的子图的关系?在学校环境中,如何计算学生 G 在特定 class 中的朋友,而不管她属于那里?

我的全局图

library(igraph)
school <- read.table(text="
                         A   B   C   D   E   F   G
                     A   0   1   0   1   0   1   1
                     B   1   0   1   1   0   1   0
                     C   0   0   0   0   0   0   1
                     D   1   1   0   0   1   0   0
                     E   0   0   0   1   0   1   1
                     F   0   1   0   0   1   0   1
                     G   1   0   1   0   1   1   0", header=TRUE)

mat <- as.matrix(school)
schoolgraph <- graph.adjacency(mat, mode="undirected", add.rownames = T)

我的子图

schoolsub <- induced.subgraph(schoolgraph,1:3)

IGRAPH 7dfb160 UN-- 3 2 -- 
+ attr: name (v/c), TRUE (v/c)
+ edges from 7dfb160 (vertex names):
[1] A--B B--C

现在,如何计算子图"subschool"中学生"G"的好友数?结果应该是一个数字(G 在 schoolsub 有两个朋友)和一个名字列表(G 是 schoolsub 的 A 和 C 的朋友)。

how do I count the number of friends of student "G" in subgraph "subschool"?

一种方法可能是

sum(schoolgraph["G",V(schoolsub)$name])
# [1] 2

slam::row_sums(schoolgraph[c("F", "G"),V(schoolsub)$name])
# F G 
# 2 2

你可以先得到邻居,然后用它们来子集schoolsub:

nbs <- neighbors(schoolgraph, "G")$name
V(schoolsub)$name[V(schoolsub)$name %in% nbs]
#[1] "A" "C"