Skip to content

Instantly share code, notes, and snippets.

@shuckle16
Last active October 9, 2021 15:36
Show Gist options
  • Save shuckle16/e7d2fb3f8291ae1c926898ef4595ce36 to your computer and use it in GitHub Desktop.
Save shuckle16/e7d2fb3f8291ae1c926898ef4595ce36 to your computer and use it in GitHub Desktop.
fast date parsing r
library(data.table)
library(tictoc)
library(lubridate)
dt <- data.table(
dayte = format(sample(Sys.Date() + 1:100, size = 1e6, replace = T), format = "%d%b%Y"),
a = sample(1000, size = 1e6, replace = T)
)
tic()
a_df <- dt[, .(dt1 = as.Date(dayte, "%d%b%Y"), a)][order(dt1, a)]
toc()
tic()
a_df2 <- merge(dt[, .(dt1 = as.Date(dayte[1], "%d%b%Y")), by = dayte], dt)[,.(dt1, a)][(order(dt1, a))]
toc()
tic()
a_df3 <- dt[, .(dt1 = as.Date(fast_strptime(dayte, format = "%d%b%Y", lt = F)), a)][order(dt1, a)]
toc()
tic()
a_df4 <- merge(dt[, .(dt1 = as.Date(fast_strptime(dayte[1], "%d%b%Y"))), by = dayte], dt)[,.(dt1, a)][(order(dt1, a))]
toc()
identical(a_df, a_df2)
identical(a_df, a_df3)
identical(a_df, a_df4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment