xml to r data extraction

僤鯓⒐⒋嵵緔 提交于 2019-12-06 08:19:29

问题


I need to extract data from xml file and plot graphs of: depth vs timestamp. Heading should be the IFC code. I tried using xmlToList and xmlTodataframe, but I failed doing so. I need help. My xml file looks like

 <document>
    <site>
       <IFC_code>HONEYCR01</IFC_code>
       <Latitude>41.960161</Latitude>
       <Longitude>-90.470759</Longitude>
       <River>Honey Creek</River>
       <Road>Hwy 136, 1st Street</Road>
       <Town>Charlotte</Town>
       <from_sensor_to_river_bottom>9.35</from_sensor_to_river_bottom>
       <Unit>foot</Unit>
    </site>
    <data>
       <value>
          <timestamp>2012-05-17 15:30:03-05</timestamp>
          <depth>8.53</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:30:06-05</timestamp>
          <depth>8.50</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:15:02-05</timestamp>
          <depth>8.51</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:00:12-05</timestamp>
          <depth>8.50</depth>
       </value>
       <value>
          <timestamp>2012-05-17 13:45:08-05</timestamp>
          <depth>8.51</depth>
       </value>
      </data>
    </document>

回答1:


It seems to work:

library(XML)
doc <- xmlParse("a.xml")
xmlToDataFrame(
  getNodeSet(doc, "//value"),
  colClasses=c("character","numeric")
)


来源:https://stackoverflow.com/questions/10644998/xml-to-r-data-extraction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!