Learn R Programming

⚠️There's a newer version (0.1.5) of this package.Take me there.

ymd

Convert ‘YMD’ format number or string to Date efficiently, e.g., 211225 to as.Date("2021-12-25"), using Rust’s standard library. It also provides helper functions to handle Date, e.g., quick finding the beginning or end of the given period, adding months to Date, etc.

It’s similar to the lubridate package but is much lighter and focuses only on Date objects.

Installation

Binary version (no Rust toolchain required)

CRAN provides the binary package. So, if you are on Windows or macOS, the package can be installed via:

install.packages("ymd")

If you are on Linux, you can try to use the RSPM (RStudio Package Manager) repo provided by RStudio PBC, via (remember to choose the correct binary repo URL for your platform):

install.packages("ymd", repos = "{RSPM-Repo-URL}")

Source version (Rust toolchain required)

If you want to build the dev version from source, you’ll need the Rust toolchain, which can be installed following the instructions from the Rust book.

After that, you can build the package via:

remotes::install_github("ymd")

Use Cases and Benchmarks

print_bmk <- function(x) {
  x[[1]] <- format(x[[1]])
  x[[5]] <- format(x[[5]])
  rnd <- \(v) if (is.numeric(v)) round(v, 1) else v
  x[, 1:8] |>
    lapply(rnd) |>
    as.data.frame() |>
    knitr::kable() |>
    print()
}
run_bmk <- function(..., time_unit = "us") {
  bench::mark(..., time_unit = time_unit) |> print_bmk()
}

ymd

x <- c("210101", "21/02/03", "89-1-03", "1989.03.05", "01 02 03")
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)39.840.624357.7810.1KB0.0100000
lubridate::ymd(x)1390.41546.6642.99.01MB10.92955
x <- c(210101, 210224, 211231, 19890103)
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)12.413.471767.63.17KB0.0100000
lubridate::ymd(x)1724.91897.4511.3365.38KB15.22357
x <- c("2021-01-01", "2022-12-31", "1995-03-22")
x <- rep(x, 100)
run_bmk(
  ymd::ymd(x),
  lubridate::ymd(x),
  as.Date(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::ymd(x)29.130.731806.32.39KB3.299991
lubridate::ymd(x)796.1891.81074.0193.52KB15.14997
as.Date(x)790.5829.71194.685.44KB0.05980
x <- ymd::ymd(210515) + 1:100
run_bmk(
  ymd::eop$tm(x),
  lubridate::ceiling_date(x, "month") - 1
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::eop$tm(x)5.76.4149613.519.3KB0.0100000
lubridate::ceiling_date(x, “month”) - 134.237.825043.7155.5KB27.6998911

edate

`%m+%` <- lubridate::`%m+%`
x <- ymd::ymd(c(200115, 200131, 200229, 200331, 200401))
x <- rep(x, 100)
run_bmk(
  ymd::edate(x, 2),
  x %m+% months(2)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::edate(x, 2)12.413.470780.56.2KB0.0100000
x %m+% months(2)1140.71219.1801.4496.8KB6.13913
run_bmk(
  ymd::edate(x, -12),
  x %m+% months(-12)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::edate(x, -12)12.513.569090.53.95KB0.0100000
x %m+% months(-12)1510.11674.2589.7310.64KB12.72786

Extract Date Part

# tweak from https://github.com/Rdatatable/data.table/pull/5300
set.seed(373L)
x <- as.Date(data.table::as.IDate(sample(seq(-25000, 45000), 1e6, TRUE)))

run_bmk(
  data.table::year(x),
  lubridate::year(x),
  funchir::quick_year(x),
  ymd::year(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::year(x)4528.24988.7172.07.64MB40.08620
lubridate::year(x)281901.9282685.43.557.23MB7.124
funchir::quick_year(x)30036.830385.827.726.76MB7.9144
ymd::year(x)7976.58543.1113.73.82MB6.0573
run_bmk(
  data.table::month(x),
  lubridate::month(x),
  ymd::month(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::month(x)21728.821979.744.87.63MB3.9232
lubridate::month(x)265033.9267228.53.795.37MB3.722
ymd::month(x)7696.78770.6111.03.82MB9.9565
run_bmk(
  data.table::quarter(x),
  lubridate::quarter(x),
  ymd::quarter(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::quarter(x)17607.517977.151.17.63MB7.9264
lubridate::quarter(x)285546.4289119.83.5110.66MB3.522
ymd::quarter(x)14518.915361.264.03.82MB4.0322
run_bmk(
  data.table::yday(x),
  lubridate::yday(x),
  funchir::quick_yday(x),
  ymd::yday(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::yday(x)8542.58857.7109.37.63MB8.0554
lubridate::yday(x)240595.5244088.83.957.23MB6.535
funchir::quick_yday(x)23482.624758.539.719.08MB13.9207
ymd::yday(x)8091.38580.1105.73.82MB8.0534
run_bmk(
  data.table::mday(x),
  lubridate::mday(x),
  funchir::quick_mday(x),
  ymd::mday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::mday(x)21263.021479.846.57.63MB4.4212
lubridate::mday(x)243894.2243894.24.149.59MB8.212
funchir::quick_mday(x)9031.99400.6106.015.28MB16.7386
ymd::mday(x)8168.18903.7112.33.82MB4.2542
run_bmk(
  data.table::wday(x),
  lubridate::wday(x),
  ymd::wday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::wday(x)2781.93030.6328.97.63MB29.013612
lubridate::wday(x)243859.8243859.84.157.22MB12.313
ymd::wday(x)9403.210239.497.93.82MB6.5453
run_bmk(
  data.table::isoweek(x),
  lubridate::isoweek(x),
  ymd::isoweek(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::isoweek(x)3606869.83606869.80.3259.48MB1.114
lubridate::isoweek(x)614492.8614492.81.6270.87MB6.514
ymd::isoweek(x)10148.311139.387.73.82MB4.0442

Session Info

xfun::session_info()
#> R version 4.4.1 (2024-06-14)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Sonoma 14.6
#> 
#> Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#> 
#> Package version:
#>   base64enc_0.1.3   bench_1.1.3       bslib_0.8.0       cachem_1.1.0     
#>   cli_3.6.3         compiler_4.4.1    cpp11_0.4.7       data.table_1.15.4
#>   digest_0.6.36     evaluate_0.24.0   fansi_1.0.6       fastmap_1.2.0    
#>   fontawesome_0.5.2 fs_1.6.4          funchir_0.2.2     generics_0.1.3   
#>   glue_1.7.0        graphics_4.4.1    grDevices_4.4.1   highr_0.11       
#>   htmltools_0.5.8.1 jquerylib_0.1.4   jsonlite_1.8.8    knitr_1.48       
#>   lifecycle_1.0.4   lubridate_1.9.3   magrittr_2.0.3    memoise_2.0.1    
#>   methods_4.4.1     mime_0.12         pillar_1.9.0      pkgconfig_2.0.3  
#>   profmem_0.6.0     R6_2.5.1          rappdirs_0.3.3    rlang_1.1.4      
#>   rmarkdown_2.27    sass_0.4.9        stats_4.4.1       tibble_3.2.1     
#>   timechange_0.3.0  tinytex_0.52      tools_4.4.1       utf8_1.2.4       
#>   utils_4.4.1       vctrs_0.6.5       xfun_0.46         yaml_2.3.10      
#>   ymd_0.1.1

Copy Link

Version

Install

install.packages('ymd')

Monthly Downloads

382

Version

0.1.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Xianying Tan

Last Published

August 23rd, 2024

Functions in ymd (0.1.2)

beop

Find the Beginning or End of Period
edate

Calculate the date before / after months
ymd

Convert 'YMD' format integer or string to Date
date_part

Fast Date Part Extracting