Parsing a large amount of characters into a POSIXct object

September 22, 2015
By

(This article was first published on NumberTheory » R stuff, and kindly contributed to R-bloggers)

When trying to parse a large amount of datetime characters into POSXIct objects, it struck me that strftime and as.POSIXct where actually quite slow. When using the parsing functions from lubridate, these where a lot faster. The following benchmark shows this quite nicely.

We have a character vector xi, which contains about 2.3 million elements. When using strftime, this takes about 105 seconds on my MacBook pro:

> xi[1:10]
 [1] "2015-06-03 16:17:03" "2015-06-03 16:17:36" "2015-06-03 16:18:09"
 [4] "2015-06-03 16:18:48" "2015-06-03 16:19:21" "2015-06-03 16:19:54"
 [7] "2015-06-03 16:20:27" "2015-06-03 16:21:00" "2015-06-03 16:21:32"
[10] "2015-06-03 16:22:05"

> system.time(res <- as.POSIXct(xi))
   user  system elapsed 
104.941   1.054 106.201

If we switch to ymd_hms from lubridate, we get a very large performance increase:

> library(lubridate)
> system.time(res <- ymd_hms(xi))
   user  system elapsed 
  0.741   0.136   0.878

ymd_hms is about a 120 times faster.

To leave a comment for the author, please follow the link and comment on their blog: NumberTheory » R stuff.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)