R:创建xts对象改变时间格式
> str(s)
POSIXct[1:6630], format: "2011-02-14 09:31:00" "2011-02-14 09:32:00" "2011-02-14 09:33:00" "2011-02-14 09:34:00" ...
> head(s)
[1] "2011-02-14 09:31:00 EST" "2011-02-14 09:32:00 EST" "2011-02-14 09:33:00 EST" "2011-02-14 09:34:00 EST"
[5] "2011-02-14 09:35:00 EST" "2011-02-14 09:36:00 EST"
>
> head(AMKR)
date_holding time_holding close
1 2011-02-14 09:31:00 7.66
2 2011-02-14 09:32:00 7.58
3 2011-02-14 09:33:00 7.54
4 2011-02-14 09:34:00 7.54
5 2011-02-14 09:35:00 7.56
6 2011-02-14 09:36:00 7.54
> as.xts(AMKR[,-1:-2], s) -> newAMKR
> head(newAMKR)
[,1]
2011-02-14 00:00:00 7.74
2011-02-14 00:01:00 7.74
2011-02-14 00:02:00 7.74
2011-02-14 00:03:00 7.74
2011-02-14 00:04:00 7.74
2011-02-14 00:05:00 7.73
如您所见,时间已经改变。
代码“AMKR”是从位于同一台计算机上的 mySQL 数据库中提取的。
如果有人能指出我正确的方向,我将不胜感激。
====================================================
更新:有几个问题。
1) 将数据上传到 mySQL 时,时间格式设置为 %h:%i:%s,而本应设置为 %H:%i:%s(注意大写 H)。
2)我删除了mySQL中的数据并使用H重新上传。现在,我得到中午 12 点发生的报价的时间为 12:00:00,但似乎 as.xts 将此解释为上午 12 点,因此 12:00:00 和 04:00:00 之间发生的所有报价都被解释为发生在上午。
我想知道为什么 mySQL 没有将时间转换为 24 小时时钟格式,因为我在 LOAD DATA 查询中使用了 %H。我想知道是否有办法通过 as.POSIXct 强制 R 中的更改。正在调查......
> str(s)
POSIXct[1:6630], format: "2011-02-14 09:31:00" "2011-02-14 09:32:00" "2011-02-14 09:33:00" "2011-02-14 09:34:00" ...
> head(s)
[1] "2011-02-14 09:31:00 EST" "2011-02-14 09:32:00 EST" "2011-02-14 09:33:00 EST" "2011-02-14 09:34:00 EST"
[5] "2011-02-14 09:35:00 EST" "2011-02-14 09:36:00 EST"
>
> head(AMKR)
date_holding time_holding close
1 2011-02-14 09:31:00 7.66
2 2011-02-14 09:32:00 7.58
3 2011-02-14 09:33:00 7.54
4 2011-02-14 09:34:00 7.54
5 2011-02-14 09:35:00 7.56
6 2011-02-14 09:36:00 7.54
> as.xts(AMKR[,-1:-2], s) -> newAMKR
> head(newAMKR)
[,1]
2011-02-14 00:00:00 7.74
2011-02-14 00:01:00 7.74
2011-02-14 00:02:00 7.74
2011-02-14 00:03:00 7.74
2011-02-14 00:04:00 7.74
2011-02-14 00:05:00 7.73
As you can see the time has changed.
The ticker 'AMKR' is extracted from mySQL db located on the same machine.
If anyone can point me in the right direction that would be greatly appreciated.
================================================
Update: There were a couple of problems.
1) The upload of the data into mySQL was done with time format set to %h:%i:%s when it should have been set to %H:%i:%s (note the capital H).
2) I deleted the data in mySQL and re-uploaded using H. Now I get a time of 12:00:00 for a quote occurring at 12pm but it appears as.xts interprets this as 12am and so all quotes that occur between 12:00:00 and 04:00:00 are interpreted to occur in the am.
I wonder why mySQL did not convert the time to the 24-hour clock format as I did use %H in the LOAD DATA query. I wonder if there is a way to force the change in R via as.POSIXct. Investigating.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题所在。我需要使用以下时间格式将数据上传到 mySQL:'%h:%i:%s %p'。我之前遗漏了%p。另外,当您使用 %p 时,您需要使用小写的 %h。
I figured out the problem. I need to upload the data into mySQL using the following time format: '%h:%i:%s %p'. I previously left out the %p. Also when you use the %p you need to use lower case %h.