Search code examples
rstatisticsdata-scienceadjacency-matrix

Network object and adjacency matrix in R


I have a proximity network which contains the following information:

Abigail lives close to Frank

Abigail lives close to Cameron

Billy lives close to Daniell

Ethan lives close to Cameron

Ethan lives close to Frank

Frank lives close to Cameron

Could someone please help me with the code to make this into an adjacency matrix, with a vertex.names node attribute?


Solution

  • The data as you have it is basically an edge list. You can use igraph to turn that into a graph and then turn it into an adjacency matrix.

    EL = as.matrix(read.table(text="
    Abigail Frank
    Abigail Cameron
    Billy Daniell
    Ethan Cameron
    Ethan Frank
    Frank Cameron"))
    
    library(igraph)
    
    g = graph_from_edgelist(EL, directed = FALSE)
    as.matrix(as_adjacency_matrix(g))
    
            Abigail Frank Cameron Billy Daniell Ethan
    Abigail       0     1       1     0       0     0
    Frank         1     0       1     0       0     1
    Cameron       1     1       0     0       0     1
    Billy         0     0       0     0       1     0
    Daniell       0     0       0     1       0     0
    Ethan         0     1       1     0       0     0