R tick data : merging date and time into a single object

落爺英雄遲暮 提交于 2019-11-26 05:56:48

问题


I\'m currently working in tick data with R and I would like to merge date and time into a single object as I need to get a precise time object to compute some statistics on my data. Here is how my data looks like:

               date       time      price flag    exchange
2   XXH10   2010-02-02   08:00:03   2787 1824        E
3   XXH10   2010-02-02   08:00:04   2786    3        E
4   XXH10   2010-02-02   08:00:04   2787    6        E
5   XXH10   2010-02-02   08:00:04   2787    1        E
6   XXH10   2010-02-02   08:00:04   2787    1        E

Basically, I would like to merge the columns \"date\" and \"time\" into a single one.


回答1:


Create a datetime object with as.POSIXct:

as.POSIXct(paste(x$date, x$time), format="%Y-%m-%d %H:%M:%S")
[1] "2010-02-02 08:00:03 GMT" "2010-02-02 08:00:04 GMT" "2010-02-02 08:00:04 GMT"
[4] "2010-02-02 08:00:04 GMT" "2010-02-02 08:00:04 GMT"



回答2:


Of course, more elegant solution (arguably) is possible with extra package. When working with dates it's lubridate package:

library(lubridate)

with(x, ymd(date) + hms(time))

should produce POSIXlt vector.



来源:https://stackoverflow.com/questions/11609252/r-tick-data-merging-date-and-time-into-a-single-object

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