Search code examples
rgraph-theoryedge-list

Creating new edge list based on common vertices from bipartite network edge list


I have an edge list of two sets of nodes like so:

edgelist <- data.frame(from = sample(letters, 1000, replace = T), 
                       to = sample(1:50, 1000, replace = T))

What I need is to do is to produce a graph for each of the sets on nodes separately based on the common edges they have. For example, if "48" in the edge list is found in both "a" and "g", I want to have an "a-g" edge in the weighted graph of letters... Same thing for the graph of numbers. I would be grateful if you helped in creating the edge list or the graphs.


Solution

  • First, note that you probably want to wrap your example edgelist into a unique() call to avoid duplicated edges.

    If I understand well you can solve your problem with a simple self-join (although there's maybe a more efficient out-of-the-box method in igraph). For instance, using dplyr:

    library(dplyr)
    
    letters_edges <- edgelist %>%
      left_join(edgelist, by = "to") %>%
      select(from.x, from.y) %>%
      unique %>%
      filter(from.x < from.y)