58

我目前正在使用 R 处理刻度数据,我想将日期和时间合并到一个对象中,因为我需要获得一个精确的时间对象来计算我的数据的一些统计数据。这是我的数据的样子:

               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

基本上,我想将“日期”和“时间”列合并为一个。

4

2 回答 2

87

创建一个datetime对象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"
于 2012-07-23T09:13:16.547 回答
14

当然,更优雅的解决方案(可以说)可以通过额外的包来实现。使用日期时,它是lubridate包:

library(lubridate)

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

应该产生POSIXlt向量。

更新:

还有另一种使用通用日期和时间转换包的解决方案anytime(基于 C++ 库Boost date_time):

 library(anytime)

 with(x, anytime(paste(date, time)))

事实上,anytime与基础 R 和lubridate(当之无愧地被认为相当慢 - 请参阅为什么我的函数在 lubridate 日期这么慢?)C++(anytime)胜出:

 x = read.csv(text = 'date,time
2010-02-02,08:00:03
2010-02-02,08:00:04
2010-02-02,08:00:04
2010-02-03,08:00:04
2010-02-04,08:00:05
2010-02-04,08:00:05
2010-02-04,08:00:06
2010-02-04,08:00:07
2010-02-04,08:00:08
2010-02-04,08:00:14')

 microbenchmark::microbenchmark(
   base = with(x, as.POSIXct(paste(date, time), format="%Y-%m-%d %H:%M:%S")),
   anytime = with(x, anytime::anytime(paste(date, time))),
   lubri = with(x, lubridate::ymd(date) + lubridate::hms(time)),
   times = 1000L
)
Unit: microseconds
  expr      min        lq       mean   median        uq        max  neval
 base       71.163   91.2555   104.38747  104.785  112.1185   256.997  1000
 anytime    40.508   52.5385   63.46973   61.843   68.5730    221.076  1000
 lubri      1596.490 1850.4400 2235.34254 1909.588 2033.096   110751.622  1000
于 2017-05-25T15:06:56.737 回答