【问题标题】:matrix node with same attribute transformation to matrix in igraph矩阵节点与igraph中的矩阵具有相同的属性转换
【发布时间】:2021-09-28 14:50:36
【问题描述】:

我在 igraph 中有一个矩阵,如上图中的第一个。数字表示合作时间。我有一个属性,作为第二个。我想要一个可以自动为我提供 igraph 中的第三个矩阵或 R 中的其他矩阵的代码。在 adavnce 中的 Thx。

【问题讨论】:

    标签: r matrix igraph


    【解决方案1】:

    igraph方法

    你可以试试下面的代码(不需要igraph

    > +((with(a, outer(type, type, `==`)) * mat) > 0)
      A B C
    A 0 0 1
    B 0 0 0
    C 1 0 0
    

    igraph 接近

    如果您仍然想使用igraph,一种可能的选择可能是

    graph_from_adjacency_matrix(mat > 0, mode = "undirected") %>%
        get.data.frame() %>%
        filter(a[from, ] == a[to, ]) %>%
        graph_from_data_frame(directed = FALSE, vertices = row.names(a)) %>%
        get.adjacency(sparse = FALSE)
    

    给了

      A B C
    A 0 0 1
    B 0 0 0
    C 1 0 0
    

    数据

    mat <- `dimnames<-`(
        matrix(c(0, 2, 3, 2, 0, 1, 3, 1, 0), 3),
        rep(list(LETTERS[1:3]), 2)
    )
    
    a <- `rownames<-`(data.frame(type = c("edu", "company", "edu")), LETTERS[1:3])
    

    【讨论】:

    • 非常感谢。我还有一个问题。
    • @JovieChuang 什么问题?
    猜你喜欢
    • 2021-05-25
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多