【发布时间】:2021-06-20 04:52:42
【问题描述】:
我想为网络创建一个度数相关矩阵,其中的列和行捕获网络的度数。我不是在寻找一个全局度量——比如 assortativity_degree(),而是一个实际的相关矩阵,其中矩阵中的每个元素都是图中存在的节点的边数,其中度数 = 任意,度数 = 任意。我已经在 igraph 文档中进行了挖掘并用 Google 搜索,但没有什么能完全达到我想要的。我拼凑了以下内容,这似乎可行,但我想知道是否有更直接的方法我不知道。我不认为我所追求的东西是如此深奥以至于没有其他人想到它——也许 igraph 之类的函数中有一个函数,我只是不太清楚它叫什么。
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
g <- make_graph("Zachary")
x <- sort(unique(degree(g))) # vector of all degrees in the network
y <- sort(unique(degree(g))) # vector for all degrees in the network
datalist = list()
# this loop creates a vector that identifies the number of
# edges that occur between nodes of degree whatever and degree whatever
for(i in y) {
row <- mapply(function(x)
{length(E(g)[V(g)[degree(g) == i] %--% V(g)[degree(g) == x]])},
x)
datalist[[i]] <- row
}
# takes the data list created in the previous for loop and row bind it into a
# matrix
m = do.call(rbind, datalist)
# label rows and columns with the relevatn degree
rownames(m) <- unique(sort(degree(g)))
colnames(m) <- unique(sort(degree(g)))
m
#> 1 2 3 4 5 6 9 10 12 16 17
#> 1 0 0 0 0 0 0 0 0 0 1 0
#> 2 0 0 0 3 0 1 2 1 5 3 7
#> 3 0 0 2 3 1 3 1 1 0 3 2
#> 4 0 3 3 1 3 1 2 2 2 3 3
#> 5 0 0 1 3 0 1 1 2 2 2 3
#> 6 0 1 3 1 1 0 1 1 1 2 1
#> 9 0 2 1 2 1 1 0 1 0 1 0
#> 10 0 1 1 2 2 1 1 0 1 1 0
#> 12 0 5 0 2 2 1 0 1 0 0 1
#> 16 1 3 3 3 2 2 1 1 0 0 0
#> 17 0 7 2 3 3 1 0 0 1 0 0
由reprex package (v2.0.0) 于 2021-06-19 创建
【问题讨论】: