首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何提取R中矩阵中的链数?

如何提取R中矩阵中的链数?
EN

Stack Overflow用户
提问于 2016-09-21 13:48:37
回答 1查看 42关注 0票数 0

我想提取矩阵中的链数。

问题;

代码语言:javascript
复制
       [,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连接。从5到6到8。另外,第二行中的2与4到7相连。因此,

代码语言:javascript
复制
[1] 1 3 5 6 8

[1] 2 4 7
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-21 13:59:05

您可以检查igraph包:

代码语言:javascript
复制
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"

数据

代码语言:javascript
复制
# dput(mat)
# structure(c(1L, 2L, 3L, 5L, 4L, 6L, 3L, 4L, 5L, 6L, 7L, 8L), .Dim = c(6L, 2L))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39618469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档