R:邻接表到邻接矩阵 [英] R: Adjacency list to Adjacency matrix

查看:646
本文介绍了R:邻接表到邻接矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bonjour,我想将一个邻接列表(3列)转换为一个邻接矩阵.在这个论坛上,我找到了许多有关如何将边缘列表转换为邻接矩阵的示例.我成功地做到了两列列表. 我已经尝试了所有可以在网络上找到的解决方案,但是似乎缺少了一些步骤.

Bonjour, I would like to convert an adjacency list (3 columns ) to an adjacency matrix. In this forum I have found multiple examples on how to convert an edge list to an adjacency matrix. I successfully managed to do it for a two columns list. I have tried all the solutions I could find on the web but it seems that I missing a little step.

我的变量是用户,国家/地区,图书

My variables are User, Country, books

User<-c("maman","sophia","Antoine")  
Country<-c("Canada","USA","Mexico")  
books<-c("Coelho","Rimbaud","The flight") 
dat<-data.frame(User, Country,books)  


User |       Country | books   
maman |        Canada  |    Coelho   
sophia|          USA  |    Rimbaud  
Antoine|     Mexico  | The flight

第一次尝试

library(igraph)     
m<-as.matrix(dat)    
g<-graph.adjacency(m, mode="directed") ### If that worked I could have used      
"get.adjacency" 

第二次尝试

试图将数据转换为边缘列表,但由于存在三列,因此出现了错误

Second attempt

Tried to convert the data to an edge list but I got an error since there is three columns

el<-as.matrix(dat)       
g=graph.edgelist(el,directed=TRUE) # turns   

输出异常

     maman sophia Antoine Canada USA Mexico Coelho Rimbaud The fligth
maman    1   0     1       0      0   0      0        1          0
sophia   0   0     0       0      1   0      1        0          1
Antoine  0   1     1       0      1   0      0        1          0
Canada   1   0     1       0      0   1      0        1          1
 USA     0   0     0       1      0   0      0        0          1
 Mexico  0   0     0       0      1   1      1        0          0 
Coelho   0   0     1       1      0    1      0       1          0
Rimbaud  1   0     1       1      0    0      0       1          1
The fligth 0 1     0       0      1    1      0       0          1

我希望看到所有顶点之间的相互作用.类似于以下内容: http://sna.stanford.edu/sna_R_labs/output /lab_1/1.3_Krackhardt_Friendship.pdf

I would like to see the interactions between all the vertices. Something similar to this: http://sna.stanford.edu/sna_R_labs/output/lab_1/1.3_Krackhardt_Friendship.pdf

任何帮助或表示将不胜感激!!!

Any help or indication would be appreciated!!!

推荐答案

也许这就是您想要的:

m <- as.matrix(dat)
el <- cbind(m[, 1], c(m[, -1]))

在这里,el是通过将m的第一列与通过去除矩阵子集m[, 2:3]的尺寸(等效于m[, -1])而制成的矢量绑定而成的边列表.请注意,cbind中的第一个向量具有3个元素,而第二个向量具有6个元素.第一个将被回收到第二个的长度.我们所做的等同于执行cbind(rep(m[, 1], 2), m[, -1]).

Here, el is an edge list, created by binding the first column of m, with a vector that is made by removing the dimensions of the matrix subset m[, 2:3] (equivalent to m[, -1]). Note that the first vector in the cbind has 3 elements, while the second vector has 6 elements. The first will be recycled to the length of the second. What we've done is equivalent to doing cbind(rep(m[, 1], 2), m[, -1]).

这是我们的边缘列表的样子.

Here's what our edge list looks like.

el
##      [,1]      [,2]        
## [1,] "maman"   "Canada"    
## [2,] "sophia"  "USA"       
## [3,] "Antoine" "Mexico"    
## [4,] "maman"   "Coelho"    
## [5,] "sophia"  "Rimbaud"   
## [6,] "Antoine" "The flight"

我们现在可以通过用graph.edgelist绘制边缘列表并使用get.adjacency提取邻接矩阵来获得邻接矩阵.

We can now get the adjacency matrix by graphing the edge list with graph.edgelist and extracting the adjacency matrix with get.adjacency.

get.adjacency(graph.edgelist(el))

## 9 x 9 sparse Matrix of class "dgCMatrix"
##            maman Canada sophia USA Antoine Mexico Coelho Rimbaud The flight
## maman          .      1      .   .       .      .      1       .          .
## Canada         .      .      .   .       .      .      .       .          .
## sophia         .      .      .   1       .      .      .       1          .
## USA            .      .      .   .       .      .      .       .          .
## Antoine        .      .      .   .       .      1      .       .          1
## Mexico         .      .      .   .       .      .      .       .          .
## Coelho         .      .      .   .       .      .      .       .          .
## Rimbaud        .      .      .   .       .      .      .       .          .
## The flight     .      .      .   .       .      .      .       .          .

这篇关于R:邻接表到邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆