首页 文章

基于数据帧中的行标签子集igraph稀疏矩阵

提问于
浏览
1

我有一个igraph对象,它基本上是一个稀疏矩阵,其中列和行都用id标记 . 我还有一个带行标签和社区值的数据框 . 我试图通过选择与某个特定值的社区数据框中的行标签匹配的所有行和列来对邻接矩阵进行子集化 .

我已经尝试了匹配,plyr和子集的各种方法,但无法获得任何工作 . 以下是数据的两个子集 .

match(g2, communi)

>g2[1:3,1:3]
3 x 3 sparse Matrix of class "dgCMatrix"
        568120 711503 1077594
568120       .      7       4
711503       7      .       4
1077594      4      4       .

> head(communi)
        communi
568120        7
711503        7
1077594       7
1078147       7
772988      464
757866       72

1 回答

  • 2

    目前尚不清楚是否要将邻接矩阵或图形进行子集化,但这是两者的一个例子 .

    # example graph from igraph documentation
    library(igraph)   
    g <- graph.full(5) %du% graph.full(5) %du% graph.full(5)
    g <- add.edges(g, c(1,6, 1,11, 6, 11))
    
    # calcualte community structure (many ways to do this)...
    wtc <- walktrap.community(g)
    # subset the graph (only community 1)
    subgr <- induced.subgraph(g,membership(wtc)==1)
    par(mfrow=c(1,2),mar=c(0,0,0,0))
    plot(g)
    plot(subgr)
    # extract adjacency matrix from subgraph
    get.adjacency(subgr)
    # 5 x 5 sparse Matrix of class "dgCMatrix"
    #               
    # [1,] . 1 1 1 1
    # [2,] 1 . 1 1 1
    # [3,] 1 1 . 1 1
    # [4,] 1 1 1 . 1
    # [5,] 1 1 1 1 .
    

    因此,在此示例中,我们对图形进行子集化,然后从中提取邻接矩阵 .

相关问题