简体   繁体   English

r从邻接表创建邻接矩阵或边列表

[英]r create adjacency matrix or edge list from adjacency list

I have an adjacency list and I am trying to make it into and adjacency matrix or edge list. 我有一个邻接列表,并且尝试将其放入邻接矩阵或边缘列表。 This is in order to conduct network analysis on the network built from the adjacency matrix or edge list. 这是为了对从邻接矩阵或边缘列表建立的网络进行网络分析。 I am using R. An example of adjacency list is as follows (each row has different amount of entries, and the empty entries are NA): 我正在使用R。邻接表的示例如下(每行的条目数量不同,而空条目为NA):

[17,50,90,NA,NA;
80,67,NA,NA,NA;
33,31,32, NA,NA;
33,31,32,NA,NA;
354,56,87,97,32;
....]

I tried using R: Adjacency list to Adjacency matrix but this only works if my adjacency list has two entries (ie there are more than two neighbors in a group). 我尝试使用R:邻接列表到邻接矩阵,但这仅在邻接列表有两个条目(即,一个组中有两个以上的邻居)时才有效。 I get an edge list but only taking into account the first two entries in my list. 我得到一个边缘列表,但只考虑了列表中的前两个条目。

I also tried using From list to adjacency matrix but using igraph and make_graph(unlist(mydata)) led to the error: "At type_indexededgelist.c:117 : cannot create empty graph with negative number of vertices, Invalid value" 我还尝试使用“ 从列表到邻接矩阵”,但使用igraphmake_graph(unlist(mydata))导致错误: "At type_indexededgelist.c:117 : cannot create empty graph with negative number of vertices, Invalid value"

I need an adjacency matrix which takes into account the weights that would be in the network (like if entry 31 and 32 are in two rows, then their edge weight would be 2). 我需要一个考虑到网络中权重的邻接矩阵(例如,如果条目31和32位于两行中,则它们的边缘权重将为2)。 Thank you for any help. 感谢您的任何帮助。

I have an adjacency list and I am trying to make it into an[...] edge list. 我有一个邻接列表,我正在尝试使其成为边缘列表。

Maybe you could do 也许你可以做

lst <- read.csv(header=F, comment.char=";", colClasses="integer", text="
17,50,90,NA,NA;
80,67,NA,NA,NA;
33,31,32,NA,NA;
33,31,32,NA,NA;
354,56,87,97,32;")
m <- as.matrix(lst)
e <- stack(split(m, row(m)))[,2:1]
(e <- e[complete.cases(e),])
# 1    1     17
# 2    1     50
# 3    1     90
# 6    2     80
# 7    2     67
# 11   3     33
# 12   3     31
# 13   3     32
# 16   4     33
# 17   4     31
# 18   4     32
# 21   5    354
# 22   5     56
# 23   5     87
# 24   5     97
# 25   5     32

or 要么

e <- apply(lst, 1, function(x)x[!is.na(x)])
(e <- do.call(rbind, lapply(e, embed, 2))[,2:1])
#      [,1] [,2]
# [1,]   17   50
# [2,]   50   90
# [3,]   80   67
# [4,]   33   31
# [5,]   31   32
# [6,]   33   31
# [7,]   31   32
# [8,]  354   56
# [9,]   56   87
# [10,]   87   97
# [11,]   97   32

depending on what you need/what your "adjacency list" represents. 取决于您的需要/“邻接表”代表什么。

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

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