数据:将日期和时间合并到一个对象中

时间:2021-12-01 12:38:26

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:

我目前正在用R处理tick数据,我想将日期和时间合并到一个对象中,因为我需要一个精确的时间对象来计算我的数据的一些统计信息。我的数据如下:

               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.

基本上,我想将“日期”和“时间”这两个列合并到一个单独的列中。

2 个解决方案

#1


48  

Create a datetime object with as.POSIXct:

使用as.POSIXct创建一个datetime对象:

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


0  

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.

应该生产POSIXlt向量。

#1


48  

Create a datetime object with as.POSIXct:

使用as.POSIXct创建一个datetime对象:

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


0  

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.

应该生产POSIXlt向量。