3
votes

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.

What I tried

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

First attempt

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   

Excepted output

     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

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!!!

1

1 Answers

3
votes

Maybe this is what you're after:

m <- as.matrix(dat)
el <- cbind(m[, 1], c(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"

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     .      .      .   .       .      .      .       .          .