![](/img/trans.png)
[英]R and igraph: subgraph nodes based on attributes of other nodes that are incident on edge
[英]r igraph - Identify ties of nodes to a subgraph regardless of affiliation to said subgraph
如何计算节点与同一图的子图的联系? 在学校中,如何计算学生G的朋友在特定班级中的收入,而不论她属于哪个班级?
我的全球图
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的朋友)。
如何计算“ subschool”子图中“ G”学生的朋友数量?
一种方法可能是
sum(schoolgraph["G",V(schoolsub)$name])
# [1] 2
要么
slam::row_sums(schoolgraph[c("F", "G"),V(schoolsub)$name])
# F G
# 2 2
您可以先得到邻居,然后使用它们来对schoolsub
进行子集schoolsub
:
nbs <- neighbors(schoolgraph, "G")$name
V(schoolsub)$name[V(schoolsub)$name %in% nbs]
#[1] "A" "C"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.