【问题标题】:How to extract the chaining numbers in a matrix in R?如何在R中的矩阵中提取链接数?
【发布时间】:2016-09-21 13:48:37
【问题描述】:

我想提取矩阵中的链数。

问题;

       [,1] [,2]
[1,]    1    3
[2,]    2    4
[3,]    3    5
[4,]    5    6
[5,]    4    7
[6,]    6    8

例如,第一行第二个数字3,连接第三行的3,然后第二列的5,第三行的第五行的5。 5 到 6 到 8 连续。另外,第二行的 2 与 4 到 7 连接。结果,

[1] 1 3 5 6 8

[1] 2 4 7

【问题讨论】:

    标签: r matrix extract


    【解决方案1】:

    你可以查看igraph包:

    library(igraph)
    g <- graph.data.frame(as.data.frame(mat))   # convert the matrix to data frame and graph object
    m <- clusters(g)   # calculate the clusters of the graph based on connections
    lapply(split(m$membership, m$membership), names)  # split nodes based on their membership 
                                                      # and extract the name of the nodes
    
    # $`1`
    # [1] "1" "3" "5" "6" "8"
    
    # $`2`
    # [1] "2" "4" "7"
    

    数据

    # dput(mat)
    # structure(c(1L, 2L, 3L, 5L, 4L, 6L, 3L, 4L, 5L, 6L, 7L, 8L), .Dim = c(6L, 2L))
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2018-09-25
      • 2022-09-22
      • 2013-03-18
      相关资源
      最近更新 更多