简体   繁体   English

字图-标识节点与子图的联系,而与该子图的从属关系无关

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

How to count the ties of a node to a subgraph of the same graph? 如何计算节点与同一图的子图的联系? In a school context, how to count student G's friends in a specific class, regardless of her belonging there? 在学校中,如何计算学生G的朋友在特定班级中的收入,而不论她属于哪个班级?

My global graph 我的全球图

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)

My subgraph 我的子图

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

Now, how do I count the number of friends of student "G" in subgraph "subschool"? 现在,如何计算子图中“ subschool”中学生“ G”的朋友数? The results should be a number (G has two friends in schoolsub) and a list of names (G is friends with A and C of schoolsub). 结果应该是一个数字(G在schoolsub中有两个朋友)和一个名称列表(G是在schoolsub中有A和C的朋友)。

how do I count the number of friends of student "G" in subgraph "subschool"? 如何计算“ subschool”子图中“ G”学生的朋友数量?

One way could be 一种方法可能是

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

or 要么

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

You can get the neighbours first, and use them to subset schoolsub : 您可以先得到邻居,然后使用它们来对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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM