【问题标题】:Converting a custom 2D sparse matrix into a network将自定义 2D 稀疏矩阵转换为网络
【发布时间】:2018-05-13 15:48:44
【问题描述】:

我有这样一个从程序中导出的数组,看起来像:

1 2:1.827411e-02 3:5.355330e-02 4:1.827411e-02 5:1.827411e-02
2 1:1.827411e-02 3:1.903553e-02 4:4.568528e-03 5:4.568528e-03
3 1:5.355330e-02 2:1.903553e-02 4:1.903553e-02 5:1.903553e-02 6:7.461929e-02 11:3.350254e-02
4 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 5:4.568528e-03
5 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 4:4.568528e-03
6 3:7.461929e-02 7:1.903553e-02 8:1.903553e-02 9:5.355330e-02 10:1.903553e-02 11:3.350254e-02
7 6:1.903553e-02 8:4.568528e-03 9:1.827411e-02 10:4.568528e-03
8 6:1.903553e-02 7:4.568528e-03 9:1.827411e-02 10:4.568528e-03
9 6:5.355330e-02 7:1.827411e-02 8:1.827411e-02 10:1.827411e-02
10 6:1.903553e-02 7:4.568528e-03 8:4.568528e-03 9:1.827411e-02
11 3:3.350254e-02 6:3.350254e-02

每行描述行的第一个数字和冒号之前的数字之间的边的权重(冒号之后)。

即第一行:

the weight between 1 and 2 is 1.827411e-02
the weight between 1 and 3 is 5.355330e-02
the weight between 1 and 4 is 1.827411e-02
the weight between 1 and 5 is 1.827411e-02

其余行以此类推。

这个信息我想以某种方式将其转换为 igraph 或网络元素,以便对其进行更多分析。有没有有效的方法来做到这一点?

【问题讨论】:

    标签: r graph type-conversion sparse-matrix


    【解决方案1】:

    您可以使用一点正则表达式来提取相关值以形成加权边缘列表。

    # parent node : grab the first number
    parent <- sub("(\\d+ ).*$", "\\1", r) 
    
    # child node: grab everything after the first number and split it
    child <- strsplit(sub("\\d+ (.*$)", "\\1", r), ":| ")
    
    # cbind the parent node to the child    
    dat2 <- cbind(rep(parent, lengths(child)/2), matrix(unlist(child), nc=2, byrow=TRUE))
    
    mode(dat2) = "numeric" # change to numeric
    
    
    # read in a graph: the weights are in the edge attributes
    g <- igraph::graph_from_data_frame(dat2)
    

    数据

    假设您的数据在文本文件中采用这种形式(将 textConnection(txt) 更改为文件路径)

    txt <- 
      '1 2:1.827411e-02 3:5.355330e-02 4:1.827411e-02 5:1.827411e-02
    2 1:1.827411e-02 3:1.903553e-02 4:4.568528e-03 5:4.568528e-03
    3 1:5.355330e-02 2:1.903553e-02 4:1.903553e-02 5:1.903553e-02 6:7.461929e-02 11:3.350254e-02
    4 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 5:4.568528e-03
    5 1:1.827411e-02 2:4.568528e-03 3:1.903553e-02 4:4.568528e-03
    6 3:7.461929e-02 7:1.903553e-02 8:1.903553e-02 9:5.355330e-02 10:1.903553e-02 11:3.350254e-02
    7 6:1.903553e-02 8:4.568528e-03 9:1.827411e-02 10:4.568528e-03
    8 6:1.903553e-02 7:4.568528e-03 9:1.827411e-02 10:4.568528e-03
    9 6:5.355330e-02 7:1.827411e-02 8:1.827411e-02 10:1.827411e-02
    10 6:1.903553e-02 7:4.568528e-03 8:4.568528e-03 9:1.827411e-02
    11 3:3.350254e-02 6:3.350254e-02'
    
    r <- readLines(textConnection(txt))
    

    【讨论】:

    • 非常感谢您的回答。你知道我如何在绘图中考虑权重吗?我使用了plot.igraph( g, edge.width=E(g)$weight),似乎没有改变边缘的宽度。
    • 由于权重相似,您可能不会看到太大差异。您可以重新缩放,即 E(g)$weight2 &lt;- X* E(g)$weight / max(E(g)$weight) ,并乘以一些常数 X(例如,X=10),这样可以看到差异
    猜你喜欢
    • 2023-04-10
    • 2021-11-25
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多