【问题标题】:Subsetting igraph sparse matrix based on row labels in a data frame基于数据框中的行标签对 igraph 稀疏矩阵进行子集化
【发布时间】:2014-11-20 00:58:01
【问题描述】:

我有一个 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

【问题讨论】:

  • 你能上传你的矩阵和数据框,或者一个例子吗?
  • 添加了示例数据。通信数据框可以很容易地转换为列表或其他对象,而 igraph 对象则不然。

标签: r igraph subset


【解决方案1】:

不清楚是要对邻接矩阵还是图形进行子集化,但这里是两者的示例。

# 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 .

所以在这个例子中,我们对图进行子集化,然后从中提取邻接矩阵。

【讨论】:

  • 我想要子图,但认为有必要首先对邻接矩阵进行子集化。感谢您提供此解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
相关资源
最近更新 更多