Alex Rivera | Logout

convert string date to R Date FAST for all dates

Asked 2013-01-08T15:25:15.823
12

This has been asked several times with no clear answer: I would like to convert an R character string of the form "YYYY-mm-dd" into a Date. The as.Date function is exceedingly slow. convert character to date *quickly* in R provides a solution using fasttime that works for dates from 1970 onward. My issue is I have dates starting from 1900 that I need to convert and there are about 100 million of them. I have to do this frequently so the speed is important. Are there any other solutions?

Edit
Report

1 Answer

10

I had a similar problem a while ago and came up with the following solution:

  1. convert the string to a factor (if not already a factor)
  2. convert the levels of the factor to a Date
  3. Expand the converted levels to the solution using the index vector of the factor

Extending Joshua Ulrich's example, I get (with slower timings on my laptop)

library(date)
set.seed(21)
x <- as.character(Sys.Date()-sample(40000, 1e6, TRUE))
system.time(dDate <- as.Date(x))
#    user  system elapsed 
#    12.09   0.00   12.12 
system.time(ddate <- as.Date(as.date(x,"ymd")))
#    user  system elapsed 
#    6.97    0.04    7.05 
system.time({
    xf <- as.factor(x)
    dDate <- as.Date(levels(xf))[as.integer(xf)]
})
#    user  system elapsed 
#    1.16    0.00    1.15

Here, step 2 does not depend on the length of x once x is large enough and step 3 scales extremely well (simple vector indexing). The bottleneck should be step 1, which can be avoided if the data is already stored as a factor.

answered 2013-03-08T09:40:38.187

Your Answer