我将如何解析 R 中的 XML 文件并对数据进行基本的统计分析 [英] How would I parse the XML file in R and carry out basic Statistics Analysis on the data

查看:27
本文介绍了我将如何解析 R 中的 XML 文件并对数据进行基本的统计分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析 R 中的 XML 文件,以便我可以分析数据.我试图获得价格的均值和标准差.此外,我希望能够获得股价变化时的变化率.我尝试手动输入数据,但在日期结构方面遇到问题(我尝试了以下操作:

I am trying to parse the XML file in R, so that I can analysis the data. I am trying to get the mean and standard deviation of the price. Also I would like to be able to get the rate of change in the time of the share price changing. I have tried entering the data by hand but am having problems with the date structure ( I have tried the following:

z <- strptime ("HH:MM:SS.ms, "%H:%m:%S.%f")

但它没有工作).我知道 XML 文件只有少数几个数字,但它是一个可以自动化的过程吗?如果是这样,我需要什么包?(我是 R 的新手).任何帮助将不胜感激.

but it failed to work). I know the XML file only has a small few numbers but is it a process that could be automated and if so what packages would I need? (I am new to R). Any help would be much appreciated.

谢谢,安东尼.

<?xml version = "1.0"?>
    <Company >
    <shareprice>
    <timeStamp> 12:00:00:01</timeStamp>
    <Price>  25.02</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00:02</timeStamp>
    <Price>  15</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00:025</timeStamp>
    <Price>  15.02</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00:031</timeStamp>
    <Price>  18.25</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00:039</timeStamp>
    <Price>  18.54</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00:050</timeStamp>
    <Price> 16.52</Price>
    </shareprice>


   <shareprice>
    <timeStamp> 12:00:01:01</timeStamp>
    <Price>  17.50</Price>
   </shareprice>
</Company>

推荐答案

z <- strptime ("HH:MM:SS.ms, "%H:%m:%S.%f")

你错过了一个结束的 " 所以它是无效的语法.

you miss a closing " so it is invalid syntax.

接下来,数据是非标准的,因为我们将使用点表示 seconds.subseconds,即 12:23:34.567 来表示时间戳.毫秒可以这样解析

Next, the data is non-standard as we would use a dot for seconds.subseconds, ie 12:23:34.567 to denote a timestamp. The milliseconds can be parsed this way

> ts <- "12:00:00.050"
> strptime(ts, "%H:%M:%OS")
[1] "2010-07-09 12:00:00 CDT"
> 

所以你不仅需要先把它从XML中取出来,还需要对字符串进行转换.否则,您可以解析字符串并手动"填充 POSIXlt 时间结构.

So you not only need to get it out of XML first, but also need to convert the string. Else, you can parse the string an fill a POSIXlt time structure 'by hand'.

后记:忘了说你需要启用亚秒打印:

Postscriptum: Forgot to mention that you need to enable printing of sub-second times:

> options("digits.secs"=3)         # shows milliseconds (three digits)
> strptime(ts, "%H:%M:%OS")
[1] "2010-07-09 12:00:00.05 CDT"   # suppresses trailing zero
> 

Postscriptum 2:由于 XML 包:

Postscriptum 2: You are also in luck with respect to your file thanks to the XML package:

> library(XML)
> xmlToDataFrame("c:/Temp/foo.xml")     # save your data as c:/Temp/foo.xml
      timeStamp   Price
1   12:00:00:01   25.02
2   12:00:00:02      15
3  12:00:00:025   15.02
4  12:00:00:031   18.25
5  12:00:00:039   18.54
6  12:00:00:050   16.52
7   12:00:01:01   17.50
> 

这篇关于我将如何解析 R 中的 XML 文件并对数据进行基本的统计分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆