-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
176 lines (145 loc) · 3.9 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# ymd
<!-- badges: start -->
[![R-CMD-check](https://github.com/shrektan/ymd/workflows/R-CMD-check/badge.svg)](https://github.com/shrektan/ymd/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/ymd)](https://CRAN.R-project.org/package=ymd)
[![Downloads from the RStudio CRAN mirror](https://cranlogs.r-pkg.org/badges/ymd)](https://cran.r-project.org/package=ymd)
[![Rust Code Coverage](https://coveralls.io/repos/github/shrektan/ymd/badge.svg?branch=main)](https://coveralls.io/github/shrektan/ymd?branch=main)
<!-- badges: end -->
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:
```r
install.packages("ymd")
```
If you are on Linux, you can try to use the [RSPM (RStudio Package Manager) repo](https://packagemanager.rstudio.com)
provided by RStudio PBC, via (remember to choose the correct binary repo URL for your platform):
```r
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](https://doc.rust-lang.org/book/ch01-01-installation.html).
After that, you can build the package via:
```r
remotes::install_github("ymd")
```
## Use Cases and Benchmarks
```{r setup}
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
```{r ymd, results='asis'}
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)
)
x <- c(210101, 210224, 211231, 19890103)
x <- rep(x, 100)
run_bmk(
ymd::ymd(x),
lubridate::ymd(x)
)
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)
)
x <- ymd::ymd(210515) + 1:100
run_bmk(
ymd::eop$tm(x),
lubridate::ceiling_date(x, "month") - 1
)
```
### edate
```{r edate, results='asis'}
`%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)
)
run_bmk(
ymd::edate(x, -12),
x %m+% months(-12)
)
```
### Extract Date Part
```{r date-part, results='asis'}
# 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)
)
run_bmk(
data.table::month(x),
lubridate::month(x),
ymd::month(x)
)
run_bmk(
data.table::quarter(x),
lubridate::quarter(x),
ymd::quarter(x)
)
run_bmk(
data.table::yday(x),
lubridate::yday(x),
funchir::quick_yday(x),
ymd::yday(x)
)
run_bmk(
data.table::mday(x),
lubridate::mday(x),
funchir::quick_mday(x),
ymd::mday(x)
)
run_bmk(
data.table::wday(x),
lubridate::wday(x),
ymd::wday(x)
)
run_bmk(
data.table::isoweek(x),
lubridate::isoweek(x),
ymd::isoweek(x)
)
```
## Session Info
```{r session-info}
xfun::session_info()
```