将矩阵转换为加权边列表时缺少边值(使用 igraph)

Missing edge values when converting matrix to weighted edge list (using igraph)

我正在将矩阵对象(其中每个节点代表一个国家/地区)转换为数据框对象,以便前两列是连接边的两个节点,第三列是边值(或属性)。我的矩阵的维度是 161 x 161,所以生成的数据框应该包含 161*161 = 25,961 行。但是,下面的代码只能产生 19,628 行,我想知道这是怎么回事?如果有人能对此有所了解,我们将不胜感激。

# load data
sim <- readRDS(url("https://www.dropbox.com/s/veyjr8mdi1u6g6h/sim.rds?dl=1"))

# load igraph package and use the graph.adjacency() function

# first try with a minimal example

myAdjacencyMatrix <- matrix(runif(100),nc=10,nr=10)
g  <- graph.adjacency(myAdjacencyMatrix,weighted=TRUE)
df <- get.data.frame(g)
dim(df)
[1] 100   3

# now repeat the same procedure on sim data

g1  <- graph.adjacency(sim,weighted=TRUE)
df1 <- get.data.frame(g1)

dim(df1)
[1] 19628     3

当您检查 sim 的值时,您会看到有 6293 个零,这些零在传输到图形表示时会被跳过。

> ecount(g1)
[1] 19628
> length(sim)
[1] 25921
> sum(sim==0)
[1] 6293