R: Gephi: manipulating dataframe to use with write.gexf

前端 未结 2 1926
小鲜肉
小鲜肉 2020-12-21 12:51

I am trying to manipulate a data frame. As an example: say I have a dataframe containing customers and the shops they visit:

df = data.frame(customers = c(\"         


        
2条回答
  •  自闭症患者
    2020-12-21 13:31

    Please take a look to the function edge.list of rgexf(http://www.inside-r.org/packages/cran/rgexf/docs/edge.list). Using your example it would be something like this

    library(rgexf)

    # Your data
    df = data.frame(customers = c("a", "b", "b", "c", "c"),
                    shop_visited = c("X", "X", "Y", "X", "Z"))
    
    # Getting nodes and edges
    df2 <- edge.list(df)
    

    Looks like this

    > df2
    $nodes
      id label
    1  1     1
    2  2     2
    3  3     3
    
    $edges
         [,1] [,2]
    [1,]    1    1
    [2,]    2    1
    [3,]    2    2
    [4,]    3    1
    [5,]    3    3
    

    Finally, you can use this to write a GEXF graph

    # Building the graph
    write.gexf(nodes=df2$nodes, edges=df2$edges)
    
    
    
      
        NodosChile
        A graph file writing in R using "rgexf"
        gexf graph, NodosChile, R, rgexf
      
      
        
          
          
          
        
        
          
          
          
          
          
        
      
    
    

    Please let me know if you have any doubt george dot vega at nodoschile.org

    Best!

    George (creator of rgexf)

提交回复
热议问题