【问题标题】:Missing edge values when converting matrix to weighted edge list (using igraph)将矩阵转换为加权边缘列表时缺少边缘值(使用 igraph)
【发布时间】:2021-11-22 04:51:27
【问题描述】:

我正在将一个矩阵对象(其中每个节点代表一个国家)转换为一个数据框对象,这样前两列是连接边的两个节点,第三列是边值(或属性)。我的矩阵的维度是 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

【问题讨论】:

    标签: r dataframe matrix igraph


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢您澄清这一点。
    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多