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<
I had a similar problem a while ago and came up with the following solution:
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.