R:Gephi:操作数据帧以与write.gexf一起使用

时间:2013-04-23 15:57:26

标签: r gephi

我正在尝试操纵数据框。举个例子:假设我有一个包含客户和他们访问的商店的数据框:

df = data.frame(customers = c("a", "b", "b", "c", "c"),
                shop_visited = c("X", "X", "Y", "X", "Z"))
customers shop_visited
        a            X
        b            X
        b            Y
        c            X
        c            Z

总结这个数据框:

  • 一位客户(b)在X以及Y开店;
  • 一位客户(b)在Y以及X开店;
  • 一位客户(c)在X以及Z开店;
  • c以及Z
  • 的一位客户(X)商店

或者,更简洁:

relations = data.frame(source = c("X","Y", "X", "Z"), 
                       target = c("Y","X","Z","X"))
 source target
      X      Y
      Y      X
      X      Z
      Z      X

我正在寻找一种能够进行转换df -> relations的方法。这背后的动机是我可以在relations中使用edges作为write.gexf参数。欢呼任何帮助。

2 个答案:

答案 0 :(得分:3)

df <- data.frame(customers = c("a", "b", "b", "c", "c"),
                 shop_visited = c("X", "X", "Y", "X", "Z"))

#create an identifier df
dfnames <- data.frame(i = as.numeric(df$shop_visited), 
                      shop_visited = df$shop_visited)

library(tnet)
tdf       <- as.tnet( cbind(df[,2],df[,1]),type =  "binary two-mode tnet" )
relations <- projecting_tm(tdf, method = "sum")

# match original names
relations[["i"]] <- dfnames[ match(relations[['i']], dfnames[['i']] ) , 'shop_visited']
relations[["j"]] <- dfnames[ match(relations[['j']], dfnames[['i']] ) , 'shop_visited']

# clean up names
names(relations) <- c("source" , "target", "weight")


#> relations
#  source target weight
#1      X      Y      1
#2      X      Z      1
#3      Y      X      1
#4      Z      X      1

答案 1 :(得分:2)

请查看edge.listhttp://www.inside-r.org/packages/cran/rgexf/docs/edge.list)的rgexf函数。使用你的例子,就像这样

库(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)

看起来像这样

> 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

最后,您可以使用它来编写GEXF图

# Building the graph
write.gexf(nodes=df2$nodes, edges=df2$edges)

<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2">
  <meta lastmodifieddate="2013-08-06">
    <creator>NodosChile</creator>
    <description>A graph file writing in R using "rgexf"</description>
    <keywords>gexf graph, NodosChile, R, rgexf</keywords>
  </meta>
  <graph mode="static">
    <nodes>
      <node id="1" label="1"/>
      <node id="2" label="2"/>
      <node id="3" label="3"/>
    </nodes>
    <edges>
      <edge id="0" source="1" target="1" weight="1.0"/>
      <edge id="1" source="2" target="1" weight="1.0"/>
      <edge id="2" source="2" target="2" weight="1.0"/>
      <edge id="3" source="3" target="1" weight="1.0"/>
      <edge id="4" source="3" target="3" weight="1.0"/>
    </edges>
  </graph>
</gexf>

如果您对nodoschile.org上的george dot vega有任何疑问,请通知我

最佳!

乔治(rgexf的创造者)