如何从边缘列表创建加权邻接列表/矩阵?

时间:2013-05-16 10:25:52

标签: r igraph adjacency-list adjacency-matrix sna

我的问题很简单:我需要从边列表中创建一个邻接列表/矩阵。

我有一个存储在csv文档中的边缘列表,其中column1 = node1和column2 = node2,我希望将其转换为加权邻接列表或加权邻接矩阵。

更确切地说,这是数据的样子 - 数字只是节点ID:

node1,node2
551,548
510,512
548,553
505,504
510,512
552,543
512,510
512,510
551,548
548,543
543,547
543,548
548,543
548,542

有关如何实现从此转换为加权邻接列表/矩阵的任何提示? 这就是我以前决定这样做的方法,没有成功(Dai Shizuka提供):

dat=read.csv(file.choose(),header=TRUE) # choose an edgelist in .csv file format
el=as.matrix(dat) # coerces the data into a two-column matrix format that igraph likes
el[,1]=as.character(el[,1])
el[,2]=as.character(el[,2])
g=graph.edgelist(el,directed=FALSE) # turns the edgelist into a 'graph object'

谢谢!

4 个答案:

答案 0 :(得分:17)

此响应仅使用基准R.结果是用于表示邻接矩阵的标准矩阵。

 el  <- cbind(a=1:5, b=5:1) #edgelist (a=origin, b=destination)
 mat <- matrix(0, 5, 5)
 mat[el] <- 1
 mat
 #    [,1] [,2] [,3] [,4] [,5]
 #[1,]    0    0    0    0    1
 #[2,]    0    0    0    1    0
 #[3,]    0    0    1    0    0
 #[4,]    0    1    0    0    0
 #[5,]    1    0    0    0    0

此处mat是您从边缘列表el定义的邻接矩阵,它是向量cbind1:5的简单5:1

如果您的边缘列表包含权重,那么您需要稍微不同的解决方案。

el <- cbind(a=1:5, b=5:1, c=c(3,1,2,1,1)) # edgelist (a=origin, b=destination, c=weight)
mat<-matrix(0, 5, 5)
for(i in 1:NROW(el)) mat[ el[i,1], el[i,2] ] <- el[i,3]  # SEE UPDATE
mat
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    0    0    0    3
#[2,]    0    0    0    1    0
#[3,]    0    0    2    0    0
#[4,]    0    1    0    0    0
#[5,]    1    0    0    0    0

<强>更新

一段时间后,我意识到前一个加权边缘列表示例中的for循环(第3行)是不必要的。您可以使用以下向量化操作替换它:

mat[el[,1:2]] <- el[,3]

答案 1 :(得分:14)

我在问题(https://sites.google.com/site/daishizuka/toolkits/sna/sna_data)中提到的网站上的帖子使用了igraph包,因此请确保已加载。

此外,我最近意识到igraph提供了一种更简单的方法,可以使用graph.data.frame()从边缘列表创建加权邻接矩阵。我在我的网站上更新了这个,但这是一个简单的例子:

library(igraph)
el=matrix(c('a','b','c','d','a','d','a','b','c','d'),ncol=2,byrow=TRUE) #a sample edgelist
g=graph.data.frame(el)
get.adjacency(g,sparse=FALSE)

应该这样做。 sparse = FALSE参数告诉它在邻接矩阵中显示0。 如果你真的不想使用igraph,我认为这是一个笨重的方法:

el=matrix(c('a','b','c','d','a','d','a','b','c','d'),ncol=2,byrow=TRUE) #a sample edgelist
lab=names(table(el)) #extract the existing node IDs
mat=matrix(0,nrow=length(lab),ncol=length(lab),dimnames=list(lab,lab)) #create a matrix of 0s with the node IDs as rows and columns
for (i in 1:nrow(el)) mat[el[i,1],el[i,2]]=mat[el[i,1],el[i,2]]+1 #for each row in the edgelist, find the appropriate cell in the empty matrix and add 1.

答案 2 :(得分:4)

从数据框边开始,使用igraph获取邻接矩阵:

头(边缘)

  node1 node2
1   551   548
2   510   512
3   548   553
4   505   504
5   510   512
6   552   543

library(igraph)
as.matrix(get.adjacency(graph.data.frame(edges)))

    551 510 548 505 552 512 543 553 504 547 542
551   0   0   2   0   0   0   0   0   0   0   0
510   0   0   0   0   0   2   0   0   0   0   0
548   0   0   0   0   0   0   2   1   0   0   1
505   0   0   0   0   0   0   0   0   1   0   0
552   0   0   0   0   0   0   1   0   0   0   0
512   0   2   0   0   0   0   0   0   0   0   0
543   0   0   1   0   0   0   0   0   0   1   0
553   0   0   0   0   0   0   0   0   0   0   0
504   0   0   0   0   0   0   0   0   0   0   0
547   0   0   0   0   0   0   0   0   0   0   0
542   0   0   0   0   0   0   0   0   0   0   0

答案 3 :(得分:0)

qdapTools 包的另一种可能性:

library(qdapTools)

el[rep(seq_len(nrow(el)), el[,'c']), c('a', 'b')] %>%
    {split(.[,'b'], .[,'a'])} %>%
    mtabulate()

##   1 2 3 4 5
## 1 0 0 0 0 3
## 2 0 0 0 1 0
## 3 0 0 2 0 0
## 4 0 1 0 0 0
## 5 1 0 0 0 0