2016-09-21 6 views
0

私は行列内の連鎖数を抽出したいと思う。Rの行列の連鎖数を抽出するには?

問題:例えば

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

、図3に示すように、最初の行の2番目の数字は、3行目の3と接続され、第2列と3行目5は、ファース行の5です。

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" 

データ:連続、5~6 2行目のまた8、2あなたがigraphパッケージを確認することができ

答えて

4
[1] 1 3 5 6 8 

[1] 2 4 7 

、結果として7に4と接続されている

# dput(mat) 
# structure(c(1L, 2L, 3L, 5L, 4L, 6L, 3L, 4L, 5L, 6L, 7L, 8L), .Dim = c(6L, 2L)) 
関連する問題