Learn R Programming

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)37.839.524224.5810.1KB0100000
lubridate::ymd(x)1414.81591.1587.88.98MB112685
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)4.14.7192504.83.17KB0100000
lubridate::ymd(x)1761.81925.6492.8365.38KB132286
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)27.529.129997.82.39KB0.0100000
lubridate::ymd(x)808.8899.9952.2193.52KB12.84456
as.Date(x)832.3880.81100.085.44KB0.05500
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.86.595222.919.3KB0.0100000
lubridate::ceiling_date(x, “month”) - 134.338.023779.2155.5KB23.8999010

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.813.969638.96.2KB0.0100000
x %m+% months(2)1182.51259.8767.4496.8KB6.23743
run_bmk(
  ymd::edate(x, -12),
  x %m+% months(-12)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
ymd::edate(x, -12)12.913.669390.13.95KB6.999991
x %m+% months(-12)1519.11598.7603.7310.64KB10.42905

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)4585.95416.8152.47.64MB37.67719
lubridate::year(x)294390.5299745.73.357.23MB6.724
funchir::quick_year(x)31250.731840.826.426.76MB7.5144
ymd::year(x)8761.99779.2100.23.82MB3.9512
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)22814.224120.236.07.63MB6.0183
lubridate::month(x)291692.5330627.53.095.37MB4.523
ymd::month(x)8999.59907.797.63.82MB8.0494
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)18667.219201.851.47.63MB5.9263
lubridate::quarter(x)295815.7311213.93.2110.66MB8.025
ymd::quarter(x)16038.616756.754.43.82MB1.9281
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)8636.19384.6101.17.63MB17.8519
lubridate::yday(x)256363.1259132.93.957.23MB5.823
funchir::quick_yday(x)24603.225937.337.819.08MB17.9199
ymd::yday(x)8815.99605.193.93.82MB4.0472
run_bmk(
  data.table::mday(x),
  lubridate::mday(x),
  funchir::quick_mday(x),
  ymd::mday(x)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::mday(x)21353.322150.644.57.63MB7.7234
lubridate::mday(x)255903.8272992.93.749.59MB5.523
funchir::quick_mday(x)9126.29729.887.015.28MB11.9446
ymd::mday(x)8909.19751.1100.73.82MB5.9513
run_bmk(
  data.table::wday(x),
  lubridate::wday(x),
  ymd::wday(x)
)
expressionminmedianitr.secmem_allocgc.secn_itrn_gc
data.table::wday(x)2711.53095.9322.27.63MB28.613512
lubridate::wday(x)251628.6251628.64.057.22MB4.011
ymd::wday(x)10693.711469.987.13.82MB6.5403
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)3774463.73774463.70.3259.48MB1.616
lubridate::isoweek(x)628920.6628920.61.6270.87MB6.414
ymd::isoweek(x)11175.612156.075.93.82MB2.0381

Session Info

xfun::session_info()
#> R version 4.4.2 (2024-10-31)
#> Platform: aarch64-apple-darwin20
#> Running under: macOS Sequoia 15.4
#> 
#> 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.2    cpp11_0.4.7       data.table_1.16.2
#>   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.2    grDevices_4.4.2   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.2     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.2       tibble_3.2.1     
#>   timechange_0.3.0  tinytex_0.52      tools_4.4.2       utf8_1.2.4       
#>   utils_4.4.2       vctrs_0.6.5       xfun_0.50.6       yaml_2.3.10      
#>   ymd_0.1.5

Copy Link

Version

Install

install.packages('ymd')

Monthly Downloads

382

Version

0.1.5

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Xianying Tan

Last Published

April 15th, 2025

Functions in ymd (0.1.5)

ymd

Convert 'YMD' format integer or string to Date
date_part

Fast Date Part Extracting
edate

Calculate the date before / after months
beop

Find the Beginning or End of Period