2013-06-18 39 views
0

我经常需要将(长)字符串转换为R中的日期类。我注意到这一步看起来很慢。在R中加快日期格式?

例子:

date <- c("5/31/2013 23:30", "5/31/2013 23:35", "5/31/2013 23:40", "5/31/2013 23:45", "5/31/2013 23:50", "5/31/2013 23:55") 

Date <- as.POSIXct(date, format="%m/%d/%Y %H:%M") 

这是不是一个巨大的问题,但我不知道如果我俯瞰,以提高效率的简单的路线。有关加快速度的任何提示?谢谢。

+0

http://stackoverflow.com/questions/14218297/convert-string-date-to-r-date-fast-for-all-dates –

+2

有包[快速时间](http://www.rforge。 net/fasttime /),但它需要一个特定的格式。 – Roland

+0

http://stackoverflow.com/questions/12898318/convert-character-to-date-quickly-in-r –

回答

3

由于我在写这篇文章之前曾经指出这是重复的,所以无论如何我都会将它添加为答案。基本上包fasttime可以帮助你IF你有日期1970-01-01 00:00:00他们GMT他们的格式year, month, day, hour, minute, second的。如果你可以重写你的日期格式,然后fastPOSIXct会很快:

# data 
date <- c("2013/5/31 23:30" , "2013/5/31 23:35" , "2013/5/31 23:40" , "2013/5/31 23:45") 

require(fasttime) 
# fasttime function 
dates.ft <- fastPOSIXct(date , tz = "GMT") 

# base function 
dates <- as.POSIXct(date , format= "%Y/%m/%d %H:%M")  

# rough comparison 
require(microbenchmark) 
microbenchmark(fastPOSIXct(date , tz = "GMT") , as.POSIXct(date , format= "%Y/%m/%d %H:%M") , times = 100L) 
#Unit: microseconds 
#          expr  min  lq median  uq  max neval 
#    fastPOSIXct(date, tz = "GMT") 19.598 21.699 24.148 25.5485 215.927 100 
# as.POSIXct(date, format = "%Y/%m/%d %H:%M") 160.633 163.433 168.332 181.9800 278.220 100 

但问题是,是能更快日期转换为一种格式fasttime可以接受或只使用as.POSIXct或购买更快的计算机?!

+0

根据我所见(从你的回答以及@BenBolker评论中的回答/评论,似乎没有真正简单的解决方案。感谢快速提示,购买更快的计算机?呃,它已经很快了,我只是试图留意如何在编程方面做得更好。 – rbatt