如何将从graphml中提取的数据转换为R中所需的多个列

时间:2014-10-09 03:09:40

标签: r dataframe igraph

我有一个大数据作为graphml格式:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
 http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

 <!-- property keys -->
  <key id="key1" for="node" attr.name="country" attr.type="string" />
  <key id="key2" for="node" attr.name="name" attr.type="string" />
  <key id="key3" for="node" attr.name="year" attr.type="int" />

  <graph id="G" edgedefault="undirected" parse.nodeids="canonical"  
   parse.edgeids="canonical" parse.order="nodesfirst">

   <!-- graph properties -->

   <!-- vertices -->
   <node id="n0">
   <data key="key1">USA</data>
   <data key="key2">Walton </data>
   <data key="key3">1990</data>
   </node>
   <node id="n1">
   <data key="key1">France</data>
    <data key="key2">Nizan </data>
    <data key="key3">2003</data>
    </node>
   <node id="n2">
    <data key="key1">UK</data>
    <data key="key2">Peter</data>
    <data key="key3">2008</data>
  </node>
  </graph>
  </graphml>

我使用以下方法在R中导入:

  library(igraph)

 My_graph <- read.graph("~/Desktop/data.xml", format="graphml")

然后将其转换为数据框:

  data_fram <- get.data.fram(My_graph)

现在我想将数据作为多个列读取,并将它们存储在txt文件中,如下:

 ID     Country       Name       Year
 0       USA          Walton     1990  
 1      France        Nizan      2003
 2       UK           Peter      2008

我感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试:

 df <- get.data.frame(My_graph, what='vertices')
 df$id <- as.numeric(gsub("[A-Za-z]+", "", df$id)) #if you need only the `numeric` part
 row.names(df) <- NULL
 df1 <- df[,c(4,1:3)]
 df1
 #  id country    name year
 #1  0     USA Walton  1990
 #2  1  France  Nizan  2003
 #3  2      UK   Peter 2008