lubridate can handle date & time data in an effective manner, currently it requires the largest unit of a date to be days. This means it does not cover functionality for working with year-quarter, year-month and year-isoweek dates.
dint provides a toolkit for these 3 types of date. It stores them in an easily human readable integer format, e.q
20141 for the first quarter of 2014 and so forth. Additionally, it goes hand in hand with
lubridate in more ways than one.
dint is implemented in base R and comes with zero external dependencies. Even if you don’t work with such special dates directly, dint can still help you at formatting dates, labelling plot axes, or getting first / last days of calendar periods (quarters, months, isoweeks).
dint provides 4 different S3 classes that inherit from
date_xx (a superclass for package development purpose).
date_yq()for year-quarter dates
date_ymfor year-month dates
date_ywfor year-isoweek dates
date_y()for storing years. This is for development purpose, and does not provide notable advantage over storing year as integers.
date_xx vectors can be created using explicit constructors
It is worth mentioning that
tsibble also provides similar functions like
yearweek(). But I think they are generally not flexible in this use case.
as_date_xx coerce other classes (mainly
4.5.2 Arithmetic and Sequences
date_xx support addition, subtraction and sequence generation.
We can access components of
date_xx (e.g the quarter of a
date_yq) with accessor functions. You can also use these functions to convert between
Accessor functions in
dint are compatible with
POSIXct classes, so are
# dint accessor functions on other classes get_quarter(Sys.Date()) #>  2 get_month(ymd(20200303)) #>  3 get_isoweek(Sys.time()) #>  18 # lubridate accessor functions on date_xx classes year(q) #>  2014 quarter(q) # default to first month in 4th quarter #>  4 month(q) #>  10 day(q) # default to 1st day in that month #>  1
last_of_xx are 2 helper functions to access the first or last day within a specifit span from a
q <- date_yq(2015, 1) # the same as as.Date(q), but more explicit first_of_quarter(q) #>  "2015-01-01" last_of_quarter(q) #>  "2015-03-31" d <- ymd("20200303") # first locate the date in a isoweek, then find the first day in that isoweek first_of_isoweek(d) #>  "2020-03-02" last_of_month(d) #>  "2020-03-31"
Formatting date_xx vectors is easy and uses a subset of the placeholders of
%q for quarters)
There are some shorthands functions for formatting
4.5.5 Labelling functions in ggplot2
There are two ways of making use of
dint functionality when working with date axis in
- use scale
scale_date_**(), this is implemented by default
- pass shorthand
format_**functions to argument
labelsin any scale, this is also applicable to
q <- tibble( time = seq(date_yq(2016, 1), date_yq(2016, 4)), value = rnorm(4) ) ggplot(q) + geom_line(aes(time, value)) + scale_x_date_ym() + ggtitle("scale_x_yq() by default") -> p1 ggplot(q) + geom_line(aes(time, value)) + scale_x_date_ym(labels = format_ym_iso) + ggtitle("labels = format_ym_iso") -> p2 p1 + p2
x <- data.frame( time = seq(as.Date("2016-01-01"), as.Date("2016-08-08"), by = "day"), value = rnorm(221) ) p <- ggplot( x, aes( x = time, y = value) ) + geom_point() p + ggtitle("default") p + scale_x_date(labels = format_yq_iso) + ggtitle("date_yq_iso") p + scale_x_date(labels = format_ym_short) + ggtitle("date_ym_short") p + scale_x_date(labels = format_yw_shorter) + ggtitle("date_yw_shorter")