我知道全局图的程度,但是现在我需要找到子图中的节点的程度。因此,约翰在学校有4个朋友,但在班上有3个朋友。我该如何指示igraph数他班上的那三个朋友,而不是他学校里的其余三个朋友?
我的全球图
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)
g <- graph.adjacency(mat, mode="undirected", add.rownames = T)
我对P,Q和R类的隶属关系矩阵
x <- read.table(text="
P Q R
A 1 1 0
B 0 0 1
C 0 0 0
D 1 0 1
E 1 1 0
F 0 1 0
G 1 1 1", header=TRUE)
inc <- as.matrix(x)
ginc <- graph.incidence(inc)
我的P类子图
class_nodes <- names(which(inc[,"P"] == 1))
class_adj <- mat[class_nodes, class_nodes]
class_graph <- graph.adjacency(class_adj, mode = "undirected")
我需要计算子图“ class_graph”中的节点的程度,但只计算它们在子图中的联系,而不是全局图。
您可以使用找到P类中的所有节点(我们专门提取了名称,以便可以在其他图形对象中查找它们)。
V(ginc)[.nei("P")]$name
然后您可以使用以下方法从主图中提取连接的子集
subg <- induced.subgraph(g, V(ginc)[.nei("P")]$name)
您可以使用
degree(subg)
# A D E G
# 2 2 2 2
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句