Non*_*urt 3 r igraph adjacency-matrix weighted-graph
虽然存在一些涉及从邻接矩阵创建图的问题,但我还没有找到太多关于从加权图中提取加权邻接矩阵的信息。
假设我有以下图表:
library(igraph)
nodes <- data.frame(name=c("a","b", "c", "d", "f", "g"))
col1 <- c("a", "g", "f","f", "d","c")
col2 <- c("b", "f","c","d","a","a")
weight <- c(1,4,2,6,2,3)
edges <- cbind.data.frame(col1,col2,weight)
g <- graph.data.frame(edges, directed=F, vertices=nodes)
E(g)$weight <- weight
Run Code Online (Sandbox Code Playgroud)
如何得到图g的加权邻接矩阵?
看来实际上有很多方法可以做到这一点。也许很明显,第一种方法是仔细查看该选项的文档as_adjacency_matrix()并使用该attr选项:
as_adjacency_matrix(g,attr = "weight",sparse = T)
6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .
Run Code Online (Sandbox Code Playgroud)
但也可以输入
get.adjacency(g,attr = "weight",sparse = T)
Run Code Online (Sandbox Code Playgroud)
要不就
g[]
6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .
Run Code Online (Sandbox Code Playgroud)
即使我不确定最后一个选项的有效性范围。