简体   繁体   English

igraph从邻接表生成邻接矩阵

[英]igraph generate adjacency matrix from adjacency list

I have friendship data in an adjacency list. 我在邻接表中有友谊数据。 Every person in the sample (denoted by id) could nominate up to 5 friends (f1-f5). 样本中的每个人(用id表示)最多可以提名5个朋友(f1-f5)。 The data frame called "net_test" looks like this: 名为“ net_test”的数据帧如下所示:

    id   f1   f2   f3   f4   f5
1  1101 1113 1112   NA   NA   NA
2  1102 1111 1113 1103 1105   NA
3  1103 1105 1110   NA   NA   NA
4  1104 1115 1106 1110 1109 1112
5  1105 1103 1109 1116 1101   NA
6  1106 1121 1103 1113   NA   NA
7  1107 1106 1111   NA   NA   NA
8  1108 1104 1109   NA   NA   NA
9  1109 1114 1103 1113 1108 1120
10 1110 1101 1103 1109 1107   NA

The first row is the column number. 第一行是列号。 From this data I'd like to generate an adjacency matrix with the ids as row and column names, and entries 1 if two ids have a friendship link. 根据这些数据,我想生成一个ID为行和列名的邻接矩阵,如果两个ID之间具有友情链接,则为1。 I tried saving it as a graph first, and then generate the adjacency matrix in a second step: 我尝试先将其保存为图形,然后在第二步中生成邻接矩阵:

require(igraph)
netdat<-graph.adjlist(net_test, mode="out", duplicate=FALSE)
adjmat <- get.adjacency(netdat, type="both")

When I apply the graph.adjlist command, the following error occurs: 当我应用graph.adjlist命令时,发生以下错误:

At structure_generators.c:84 : Invalid (negative) vertex id, Invalid vertex id

Would there be another way to convert the adjacency list into an adjacency matrix? 是否有另一种方法可以将邻接表转换成邻接矩阵?

You misunderstood the commands. 您误解了命令。 The command get.adjlist takes as parameter an igraph graph object, and returns a list-type object representation of the graph. 命令get.adjlist将igraph图形对象作为参数,并返回图形的列表类型对象表示形式。 You are applying this to a data frame which is not being coerced to an igraph object. 您正在将此应用到未强制到igraph对象的数据框。

Below is the correct way to construct a igraph graph object using a data frame, and how to get various graph representations of this object. 以下是使用数据框构造igraph图形对象的正确方法,以及如何获取该对象的各种图形表示形式。

require(reshape2)
net_list <- melt( net_test, id.vars = "id")
net_list <- net_list[ !is.na(net_list$value), c("id", "value") ]
graph_o <- graph.data.frame(net_list) #This is a proper igraph graph object
#got from a data frame directly

list_rep <- get.adjlist(graph_o) #this now returns an adjacency list 
#representation of your graph
matrix_rep <- get.adjacency(graph_o) #this gives you the adjacency
#matrix as a (sparse) matrix with the row and column names as you want.

I guess the problem is, that net_test is no proper adjacency list, because it contains more than just two columns. 我猜问题是, net_test不是适当的邻接列表,因为它包含的不仅仅是两列。 So the first thing I'd do is bringing it to this form by melting: 因此,我要做的第一件事就是通过融化将其转换为这种形式:

require(reshape2)
net_list <- melt( net_test, id.vars = "id")
net_list <- net_list[ !is.na(net_list$value), c("id", "value") ]
colnames(net_list) <- c("from", "to")
graph.adjlist(net_list, mode="out", duplicate=FALSE)

# IGRAPH D--- 1121 66 --

If you just need the adjacency matrix and without further need for igraph, you could directly cast the molten net_list 如果你只需要邻接矩阵,并没有进一步需要的igraph,你可以直接cast熔融net_list

acast(net_list, from ~ to, fun.aggregate = length )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM