Search code examples
rnetworkinggraphedge-list

Edges from matching column values in R


I would like to create an edge from matching column values in a table, so basically:

   V1  V2  V3
A  1    1   0
B  1    0   1
C  0    1   1
D  1    0   1

if I had a table like so, then I would like to make an edge list where

A - B
A - D
A - C
B - C
B - D

so I would like to create an edge whenever the column values match per row. I've looked over a lot of documentation but I can't seem to figure out anything that does something similar to this. Any help would be appreciated!


Solution

  • I would try the "igraph" package after using crossprod on your data. Assuming your data.frame is called "mydf":

    out <- crossprod(t(mydf))
    out[lower.tri(out, diag=TRUE)] <- 0
    
    library(igraph)
    g <- graph.adjacency(out)
    get.edgelist(g)
    #      [,1] [,2]
    # [1,] "A"  "B" 
    # [2,] "A"  "C" 
    # [3,] "A"  "D" 
    # [4,] "B"  "C" 
    # [5,] "B"  "D" 
    # [6,] "B"  "D" 
    # [7,] "C"  "D" 
    

    If you don't want duplicates, you can use:

    g <- graph.adjacency(out > 0)
    get.edgelist(g)