问题
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