4

I am reading a CSV with fread (as it is quicker than read_csv method), timestamp column is taken as character type.

I want to convert it to POSIXct with: as.POSIXct(strptime(rawTime, "%Y-%m-%d %H:%M:%OS"))

But this POSIXct call is very slow.

enter image description here

Is there any quicker alternatetive to this?

Henrik
  • 65,555
  • 14
  • 143
  • 159
Devharsh Trivedi
  • 561
  • 8
  • 23

1 Answers1

11

We can use fastPOSIXct from fasttime

library(fasttime)
str1 <- rep("2015-01-01", 1e6)
system.time(fastPOSIXct(str1))
#   user  system elapsed 
#   0.08    0.00    0.08 

system.time(as.POSIXct(str1))
#   user  system elapsed 
#  24.80    0.26   25.33 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    @DevharshTrivedi It is just for benchmarking I just replicated the same character element 1e6 times. You could also take a sample and do it. If your character. According to `?fastPOSIXct` documentation `The order of interpretation is fixed: year, month, day, hour, minute, second. Note that only true (positive) POSIX dates (since 1970-01-01 00:00:00) are supported` – akrun Jun 12 '17 at 12:58
  • 1
    Thanks! @akrun I saw around ~40x performance boost. Simply awesome! :D :D – Devharsh Trivedi Jun 13 '17 at 06:15