/
yearquarter.R
481 lines (428 loc) · 15.2 KB
/
yearquarter.R
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# -------------------------------------------------------------------------
#' Minimal constructor for a yearquarter object
#'
# -------------------------------------------------------------------------
#' `new_yearquarter()` is a constructor for `<grates_yearquarter>` objects aimed
#' at developers.
#'
# -------------------------------------------------------------------------
#' `<yearquarter>` objects are stored as the number of quarters (starting at 0)
#' since the Unix Epoch (1970-01-01).
#'
# -------------------------------------------------------------------------
#' @param x `[integer]`
#'
#' Vector representing the number of quarters.
#'
#' `double` vectors will be converted via `as.integer(floor(x))`.
#'
#' @param xx
#'
#' \R object.
#'
# -------------------------------------------------------------------------
#' @return
#' A `<grates_yearquarter>` object.
#'
# -------------------------------------------------------------------------
#' @examples
#' new_yearquarter(1:10)
#'
# -------------------------------------------------------------------------
#' @export
new_yearquarter <- function(x = integer()) {
if (is.vector(x, "double")) {
x <- as.integer(floor(x))
} else if (!is.integer(x)) {
stop("`x` must be integer.")
}
.new_yearquarter(x = x)
}
# -------------------------------------------------------------------------
#' Constructor for yearquarter objects
#'
# -------------------------------------------------------------------------
#' `yearquarter()` is a constructor for `<grates_yearquarter>` objects.
#'
# -------------------------------------------------------------------------
#' `<grates_yearquarter>` objects are stored as the number of quarters (starting
#' at 0) since the Unix Epoch (1970-01-01).
#'
# -------------------------------------------------------------------------
#' @param year `[integer]`
#'
#' Vector representing the year associated with `quarter`.
#'
#' `double` vectors will be converted via `as.integer(floor(x))`.
#'
#' @param quarter `[integer]`
#'
#' Vector representing the quarter associated with `year.
#'
#' `double` vectors will be converted via `as.integer(floor(x))`.
#'
# -------------------------------------------------------------------------
#' @return
#' A `<grates_yearquarter>` object.
#'
# -------------------------------------------------------------------------
#' @examples
#' yearquarter(year = 2000L, quarter = 3L)
#'
# -------------------------------------------------------------------------
#' @seealso
#' `as_yearquarter()` and `new_yearquarter()`.
#'
# -------------------------------------------------------------------------
#' @export
yearquarter <- function(year = integer(), quarter = integer()) {
# check year is integerish
if (is.vector(year, "double")) {
year <- as.integer(floor(year))
} else if (!is.integer(year)) {
stop("`year` must be integer.")
}
# check quarter is integerish
if (is.vector(quarter, "double")) {
quarter <- as.integer(floor(quarter))
} else if (!is.integer(quarter)) {
stop("`quarter` must be integer.")
}
# check quarter bounded above and below
idx <- quarter < 1L | quarter > 4L
if (any(idx, na.rm = TRUE)) {
first <- which.max(idx)
stopf(
"quarters must be integer and between 1 and 4 (inclusive) or NA. Entry %d is not (it equals %d).",
first, quarter[first]
)
}
# check compatible lengths
tmp <- .recycle(year, quarter)
year <- tmp[[1L]]
quarter <- tmp[[2L]]
.yearquarter(year = year, quarter = quarter)
}
# -------------------------------------------------------------------------
#' @rdname new_yearquarter
#' @export
is_yearquarter <- function(xx) {
inherits(xx, "grates_yearquarter")
}
# -------------------------------------------------------------------------
#' Print a year-quarter object
#'
#' @param x A `<grates_yearquarter>` object.
#' @param ... Not currently used.
#'
#' @export
print.grates_yearquarter <- function(x, ...) {
# replicate the header as in vctrs
n <- length(x)
cat("<grates_yearquarter[", n, "]>\n", sep = "")
if (n)
print(format.grates_yearquarter(x))
invisible(x)
}
# -------------------------------------------------------------------------
#' @rdname print.grates_yearquarter
#' @export
format.grates_yearquarter <- function(x, ...) {
if (length(x) == 0L)
return(character(0L))
out <- as.POSIXlt(x)
out <- sprintf("%04d-Q%d", out$year + 1900L, out$mon %/% 3L + 1)
out[is.na(x)] <- NA_character_
out
}
# -------------------------------------------------------------------------
#' @exportS3Method vctrs::vec_ptype_abbr
vec_ptype_abbr.grates_yearquarter <- function(x, ...) {"yearquarter"}
#' @exportS3Method vctrs::vec_ptype_full
vec_ptype_full.grates_yearquarter <- function(x, ...) {"yearquarter"}
# -------------------------------------------------------------------------
#' Coerce an object to year-quarter
#'
#' @description
#' `as_yearquarter()` is a generic for coercing input in to `<grates_yearquarter>`.
#' Character input is first parsed using `as.Date()`. POSIXct and POSIXlt are
#' all converted, with the timezone respected.
#'
#' @param x
#'
#' \R object
#'
#' @param ...
#'
#' Only used For character input where additional arguments are passed through
#' to `as.Date()`.
#'
#' @return
#' A `<grates_yearquarter>` object.
#'
#' @examples
#' as_yearquarter(Sys.Date())
#' as_yearquarter(as.POSIXct("2019-03-04 01:01:01", tz = "America/New_York"), interval = 2)
#' as_yearquarter("2019-05-03")
#'
#' @note
#' Internally `<grates_yearquarter>` objects are stored as the number of
#' quarters (starting at 0) since the Unix Epoch (1970-01-01).
#'
#' @seealso
#' `as.Date()`
#'
#' @export
as_yearquarter <- function(x, ...) {
UseMethod("as_yearquarter")
}
# -------------------------------------------------------------------------
#' @rdname as_yearquarter
#' @export
as_yearquarter.default <- function(x, ...) {
stopf("Not implemented for class [%s].", paste(class(x), collapse = ", "))
}
# -------------------------------------------------------------------------
#' @rdname as_yearquarter
#' @export
as_yearquarter.Date <- function(x, ...) {
# convert to posixlt (this will always be UTC when called on a date)
x <- as.POSIXlt(x)
# calculate the year
yr <- x$year + 1900L
# calculate the month relative to unix epoch
mon <- (yr - 1970L) * 12L + x$mon
# TODO - could mon ever be double here? Maybe call as_yearquarter again?
.new_yearquarter(mon %/% 3L)
}
# -------------------------------------------------------------------------
#' @rdname as_yearquarter
#' @export
as_yearquarter.POSIXt <- function(x, ...) {
x <- .as_date(x)
as_yearquarter.Date(x)
}
# -------------------------------------------------------------------------
#' @rdname as_yearquarter
#' @export
as_yearquarter.character <- function(x, ...) {
out <- as.Date(x, ...)
if (all(is.na(out)))
stop("Unable to parse any entries of `x` as Dates.")
as_yearquarter.Date(x = out, ...)
}
# -------------------------------------------------------------------------
#' @rdname as_yearquarter
#' @export
as_yearquarter.factor <- function(x, ...) {
x <- as.character(x)
as_yearquarter.character(x, ...)
}
#' @export
`[.grates_yearquarter` <- function(x, ..., drop = FALSE) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
`[[.grates_yearquarter` <- function(x, ..., drop = TRUE) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
`[<-.grates_yearquarter` <- function(x, ..., value) {
if (!inherits(value, "grates_yearquarter"))
stop("Can only assign <grates_yearquarter> objects in to an <grates_yearquarter> object.")
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
`[[<-.grates_yearquarter` <- `[<-.grates_yearquarter`
# -------------------------------------------------------------------------
#' @export
rep.grates_yearquarter <- function(x, ...) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
unique.grates_yearquarter <- function(x, incomparables = FALSE, ...) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
c.grates_yearquarter <- function(..., recursive = FALSE, use.names = TRUE) {
dots <- list(...)
if (!all(vapply(dots, inherits, TRUE, what = "grates_yearquarter")))
stop("Unable to combine <grates_yearquarter> objects with other classes.")
res <- NextMethod()
.new_yearquarter(res)
}
# -------------------------------------------------------------------------
#' @export
seq.grates_yearquarter <- function(from, to, by = 1L, ...) {
if (!inherits(to, "grates_yearquarter") || length(to) != 1L)
stop("`to` must be a <grates_yearquarter> object of length 1.")
if (!.is_scalar_whole(by))
stop("`by` must be an integer of length 1.")
from <- as.integer(from)
to <- as.integer(to)
out <- seq.int(from = from, to = to, by = by)
# Ensure integer as we cannot rely on seq.int (may return double)
out <- as.integer(out)
.new_yearquarter(out)
}
# -------------------------------------------------------------------------
#' @export
as.integer.grates_yearquarter <- function(x, ...) {
unclass(x)
}
# -------------------------------------------------------------------------
#' @export
as.double.grates_yearquarter <- function(x, ...) {
as.double(unclass(x))
}
# -------------------------------------------------------------------------
#' @export
as.Date.grates_yearquarter <- function(x, ...) {
days <- .month_to_days(unclass(x) * 3L)
.Date(as.double(days))
}
# -------------------------------------------------------------------------
#' @export
as.POSIXct.grates_yearquarter <- function(x, tz = "UTC", ...) {
if (tz != "UTC") {
stop(
"<grates_yearquarter> objects can only be converted to UTC. ",
"If other timezones are required, first convert to <Date> and then proceed as desired."
)
}
x <- .month_to_days(unclass(x) * 3L)
.POSIXct(x * 86400, tz = "UTC")
}
# -------------------------------------------------------------------------
#' @export
as.POSIXlt.grates_yearquarter <- function(x, tz = "UTC", ...) {
if (tz != "UTC") {
stop(
"<grates_yearquarter> objects can only be converted to UTC. ",
"If other timezones are required, first convert to <Date> and then proceed as desired."
)
}
x <- .month_to_days(unclass(x) * 3L)
as.POSIXlt(x * 86400, tz = "UTC", origin = .POSIXct(0, tz = "UTC"))
}
# -------------------------------------------------------------------------
#' @export
as.character.grates_yearquarter <- function(x, ...) {
format.grates_yearquarter(x)
}
# -------------------------------------------------------------------------
#' @export
as.list.grates_yearquarter <- function(x, ...) {
lapply(unclass(x), `class<-`, class(x))
}
# -------------------------------------------------------------------------
#' @export
as.data.frame.grates_yearquarter <- as.data.frame.vector
# -------------------------------------------------------------------------
#' @export
min.grates_yearquarter <- function(x, ..., na.rm = FALSE) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
max.grates_yearquarter <- function(x, ..., na.rm = FALSE) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
range.grates_yearquarter <- function(x, ..., na.rm = FALSE) {
out <- NextMethod()
class(out) <- class(x)
out
}
# -------------------------------------------------------------------------
#' @export
Summary.grates_yearquarter <- function(..., na.rm = FALSE) {
stopf("`%s()` is not supported for <grates_yearquarter> objects.", .Generic)
}
# -------------------------------------------------------------------------
#' @export
Math.grates_yearquarter <- function(x, ...) {
stopf("`%s()` is not supported for <grates_yearquarter> objects.", .Generic)
}
# -------------------------------------------------------------------------
#' @export
quantile.grates_yearquarter <- function(x, type = 1, ...) {
x <- unclass(x)
x <- as.integer(quantile(x, type = type, ...))
.new_yearquarter(x)
}
# -------------------------------------------------------------------------
#' @export
Ops.grates_yearquarter <- function(e1, e2) {
op <- .Generic
if (op %in% c("==", "!=", "<", ">", "<=", ">=")) {
if (inherits(e2, "grates_yearquarter")) {
return(NextMethod())
}
stop("Can only compare <grates_yearquarter> objects with <grates_yearquarter> objects.")
}
switch(
op,
"+" = {
if (missing(e2)) {
return(e1)
} else if (inherits(e1, "grates_yearquarter") && inherits(e2, "grates_yearquarter")) {
stop("Cannot add <grates_yearquarter> objects to each other.")
} else if (inherits(e1, "grates_yearquarter") && (.is_whole(e2))) {
return(.new_yearquarter(unclass(e1) + as.integer(e2)))
} else if (inherits(e2, "grates_yearquarter") && (.is_whole(e1))) {
return(.new_yearquarter(unclass(e2) + as.integer(e1)))
}
stop("Can only add integers to <grates_yearquarter> objects.")
},
"-" = {
if (missing(e2)) {
stop("Cannot negate a <grates_yearquarter> object.")
} else if (inherits(e2, "grates_yearquarter")) {
if (!inherits(e1, "grates_yearquarter")) {
stop("Can only subtract from a <grates_yearquarter> object, not vice-versa.")
}
return(unclass(e1) - unclass(e2))
} else if (inherits(e1, "grates_yearquarter") && is.integer(e2)) {
return(.new_yearquarter(unclass(e1) - e2))
} else if (inherits(e1, "grates_yearquarter") && .is_whole(e2)) {
return(.new_yearquarter(unclass(e1) - as.integer(e2)))
}
stop("Can only subtract whole numbers and other <grates_yearquarter> objects from <grates_yearquarter> objects.")
},
stopf("%s is not compatible with <grates_yearquarter> objects.", op)
)
}
# ------------------------------------------------------------------------- #
# ------------------------------------------------------------------------- #
# -------------------------------- INTERNALS ------------------------------ #
# ------------------------------------------------------------------------- #
# ------------------------------------------------------------------------- #
.new_yearquarter <- function(x = integer()) {
structure(x, class = "grates_yearquarter")
}
.yearquarter <- function(year, quarter) {
month <- (quarter * 3L) - 2L
tmp <- (year - 1970L) * 12L + (month - 1L)
.new_yearquarter(tmp %/% 3L)
}