Question

I often need to convert (long) character strings into the date class in R. I notice that this step seems quite slow.

Example:

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")

This isn't a huge problem, but I wonder if I'm overlooking an easy route to increased efficiency. Any tips for speeding this up? Thanks.

Was it helpful?

Solution

Since I wrote this before it was pointed out this is a duplicate, I'll add it as an answer anyway. Basically package fasttime can help you IF you have dates AFTER 1970-01-01 00:00:00 AND they are GMT AND they are of the format year, month, day, hour, minute, second. If you can rewrite your dates to this format then fastPOSIXct will be quick:

#  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

But the question would be, is it quicker to transform your dates to a format fasttime can accept or just use as.POSIXct or buy a faster computer?!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top