Search code examples
rreshapegephinetwork-analysis

Preparing data for Gephi


Greeting,

I would need to prepare data for network analysis in Gephi. I have data in the following format:

MY Data

And I need data in format (Where the values represent persons that are connected through the organization):

Required format

Thank you very much!


Solution

  • I think this code should do the job. It is not the best most elegant way of doing it, but it works :)

    # Data
    x <-
      structure(
        list(
          Persons = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L),
          Organizations = c("A", "B", "E", "F", "A", "E", "C", "D", "C", "A", "E")
        ),
        .Names = c("Persons", "Organizations"),
        class = "data.frame",
        row.names = c(NA, -11L)
      )
    
    # This will merge n:n
    edgelist <- merge(x, x, by = "Organizations")[,2:3]
    
    # We don't want autolinks
    edgelist <- subset(edgelist, Persons.x != Persons.y)
    
    # Removing those that are repeated
    edgelist <- unique(edgelist)
    
    edgelist
    #>   Persons.x Persons.y
    #> 2         1         3
    #> 3         1         2
    #> 4         3         1
    #> 6         3         2
    #> 7         2         1
    #> 8         2         3
    

    HIH

    Created on 2018-01-03 by the reprex package (v0.1.1.9000).