Title: | A Toolkit for Year-Quarter, Year-Month and Year-Isoweek Dates |
---|---|
Description: | S3 classes and methods to create and work with year-quarter, year-month and year-isoweek vectors. Basic arithmetic operations (such as adding and subtracting) are supported, as well as formatting and converting to and from standard R date types. |
Authors: | Stefan Fleck [aut, cre] |
Maintainer: | Stefan Fleck <[email protected]> |
License: | MIT + file LICENSE |
Version: | 2.1.5 |
Built: | 2024-11-25 14:57:23 UTC |
Source: | CRAN |
Works exactly like subsetting base vectors via [
, but preserves the
date_xx
class and subclasses. The replacement functions [<-
and [[<-
conduct additional checks before assignment to prevent the generation of
degenerate date_xx vectors (see examples).
## S3 method for class 'date_xx' x[i] ## S3 replacement method for class 'date_yq' x[i] <- value ## S3 replacement method for class 'date_ym' x[i] <- value ## S3 replacement method for class 'date_yw' x[i] <- value ## S3 method for class 'date_xx' x[[i]] ## S3 replacement method for class 'date_yq' x[[i]] <- value ## S3 replacement method for class 'date_ym' x[[i]] <- value ## S3 replacement method for class 'date_yw' x[[i]] <- value
## S3 method for class 'date_xx' x[i] ## S3 replacement method for class 'date_yq' x[i] <- value ## S3 replacement method for class 'date_ym' x[i] <- value ## S3 replacement method for class 'date_yw' x[i] <- value ## S3 method for class 'date_xx' x[[i]] ## S3 replacement method for class 'date_yq' x[[i]] <- value ## S3 replacement method for class 'date_ym' x[[i]] <- value ## S3 replacement method for class 'date_yw' x[[i]] <- value
x |
object from which to extract element(s) or in which to replace element(s). |
i |
indices specifying elements to extract or replace. Indices are
For When indexing arrays by An index value of |
value |
A vector of the same class as |
a date_xx
vector
x <- date_yq(2016, 1:4) x[[2]] x[1] <- date_yq(2016, 3) x[2] <- 20164 # 2016, 4th quarter x[1:2] # Trying to assign illegal values for the respective date_xx type raises an error try(x[2] <- 20165) x <- date_ym(2016, 1:3) x[1] <- 201610 # October 2016 x <- date_yw(2016, 50:52) x[1] <- 201649 # 2016, week 52
x <- date_yq(2016, 1:4) x[[2]] x[1] <- date_yq(2016, 3) x[2] <- 20164 # 2016, 4th quarter x[1:2] # Trying to assign illegal values for the respective date_xx type raises an error try(x[2] <- 20165) x <- date_ym(2016, 1:3) x[1] <- 201610 # October 2016 x <- date_yw(2016, 50:52) x[1] <- 201649 # 2016, week 52
Add/Subtract Year
x %y+% y x %y-% y ## S3 method for class 'date_y' x %y+% y ## S3 method for class 'date_y' x %y-% y ## S3 method for class 'date_yq' x %y+% y ## S3 method for class 'date_yq' x %y-% y ## S3 method for class 'date_ym' x %y+% y ## S3 method for class 'date_ym' x %y-% y ## S3 method for class 'date_yw' x %y+% y ## S3 method for class 'date_yw' x %y-% y
x %y+% y x %y-% y ## S3 method for class 'date_y' x %y+% y ## S3 method for class 'date_y' x %y-% y ## S3 method for class 'date_yq' x %y+% y ## S3 method for class 'date_yq' x %y-% y ## S3 method for class 'date_ym' x %y+% y ## S3 method for class 'date_ym' x %y-% y ## S3 method for class 'date_yw' x %y+% y ## S3 method for class 'date_yw' x %y-% y
x |
a date_xx vector |
y |
an |
date_yq(2017, 1) %y+% 1 date_yq(2017, 1) %y-% 1 date_ym(2017, 1) %y+% 1 date_ym(2017, 1) %y-% 1
date_yq(2017, 1) %y+% 1 date_yq(2017, 1) %y-% 1 date_ym(2017, 1) %y+% 1 date_ym(2017, 1) %y-% 1
as_yearqtr()
and as_yearmon()
are included for interoperability with
zoo::yearqtr()
, an alternative year-quarter format that is based on a
decimal representation as opposed to dint's integer representation of
year-quarters. as_yearweek()
follows a similar idea, but there is no
corresponding S3 class in zoo. These functions were included
for cases where you need a continuous representation of date_xx
objects
other then base::Date()
(for example, they are used by scale_date_xx)
as_yearqtr(x) ## S3 method for class 'date_yq' as_yearqtr(x) ## S3 method for class 'yearqtr' as_yearqtr(x) as_yearmon(x) ## S3 method for class 'date_ym' as_yearmon(x) ## S3 method for class 'yearmon' as_yearmon(x) as_yearweek(x) ## S3 method for class 'date_yw' as_yearweek(x) ## S3 method for class 'yearweek' as_yearweek(x)
as_yearqtr(x) ## S3 method for class 'date_yq' as_yearqtr(x) ## S3 method for class 'yearqtr' as_yearqtr(x) as_yearmon(x) ## S3 method for class 'date_ym' as_yearmon(x) ## S3 method for class 'yearmon' as_yearmon(x) as_yearweek(x) ## S3 method for class 'date_yw' as_yearweek(x) ## S3 method for class 'yearweek' as_yearweek(x)
x |
any R object |
a zoo::yearqtr, zoo::yearmon or dint::yearweek
vector.
q <- date_yq(2016, 1:4) as.numeric(q) qzoo <- as_yearqtr(q) as.numeric(qzoo) m <- date_ym(2016, 1:12) as.numeric(m) mzoo <- as_yearmon(m) as.numeric(mzoo) w <- date_yw(2016, 1:52) as.numeric(w) wzoo <- as_yearweek(w) as.numeric(wzoo)
q <- date_yq(2016, 1:4) as.numeric(q) qzoo <- as_yearqtr(q) as.numeric(qzoo) m <- date_ym(2016, 1:12) as.numeric(m) mzoo <- as_yearmon(m) as.numeric(mzoo) w <- date_yw(2016, 1:52) as.numeric(w) wzoo <- as_yearweek(w) as.numeric(wzoo)
All dint objects can be coerced to base R Date or Datetime (POSIXct
)
types. The resulting date will always default to the first possible
Date/Datetime in this period.
## S3 method for class 'date_xx' as.POSIXlt(x, tz = "UTC", ...) ## S3 method for class 'date_xx' as.POSIXct(x, tz = "UTC", ...) Sys.date_yq() Sys.date_ym() Sys.date_yw() ## S3 method for class 'date_y' as.Date(x, ...) ## S3 method for class 'date_ym' as.Date(x, ...) ## S3 method for class 'date_yq' as.Date(x, ...) ## S3 method for class 'date_yw' as.Date(x, ...)
## S3 method for class 'date_xx' as.POSIXlt(x, tz = "UTC", ...) ## S3 method for class 'date_xx' as.POSIXct(x, tz = "UTC", ...) Sys.date_yq() Sys.date_ym() Sys.date_yw() ## S3 method for class 'date_y' as.Date(x, ...) ## S3 method for class 'date_ym' as.Date(x, ...) ## S3 method for class 'date_yq' as.Date(x, ...) ## S3 method for class 'date_yw' as.Date(x, ...)
x |
any R object |
tz |
a character string. The time zone specification to be used
for the conversion, if one is required. System-specific (see
time zones), but |
... |
passed on to methods |
If lubridate is loaded, methods for lubridate generics (such as
lubridate::month()
and lubridate::year()
) are also made available by
dint.
An Object of the appropriate base R type (Date
, POSIXct
, or
POSIXlt
)
as.Date(date_yq(2017, 2)) as.POSIXlt(date_yq(2017, 2)) # When coercing to datetime, the default timezone is UTC as.POSIXct(date_yq(2017, 2))
as.Date(date_yq(2017, 2)) as.POSIXlt(date_yq(2017, 2)) # When coercing to datetime, the default timezone is UTC as.POSIXct(date_yq(2017, 2))
Concatenate date_xx Objects
## S3 method for class 'date_xx' c(...)
## S3 method for class 'date_xx' c(...)
... |
|
a vector of the same date_xx
subclass as the first element of ...
c(date_yq(2000, 1:2), date_yq(2000, 3:3)) # raises an error try(c(date_yq(2000, 1:2), date_ym(2000, 1:12)))
c(date_yq(2000, 1:2), date_yq(2000, 3:3)) # raises an error try(c(date_yq(2000, 1:2), date_ym(2000, 1:12)))
Superclass for date_yq, date_ym, date_yw, and date_y.
make_date_xx
can be used to create such objects when it is not know if
month or quarter information is available.
is_date_xx()
checks for date_xx
objects.
date_xx()
is an internally used constructor
that should only be used by developers aspiring to extend the dint package.
date_xx(x, subclass) make_date_xx(y, q = NULL, m = NULL) is_date_xx(x)
date_xx(x, subclass) make_date_xx(y, q = NULL, m = NULL) is_date_xx(x)
x |
Any R object |
subclass |
subclass to assign |
y , q , m
|
Year, quarter, month. |
a date_xx
Object, except for is_date_xx()
which returns TRUE
or
FALSE
a date_xx
Object for date_xx()
, make_date_xx
is_date_xx()
returns TRUE
or FALSE
depending on whether its
argument is of type date_xx
or not.
make_date_xx(2017) make_date_xx(2017, 4) x <- make_date_xx(2017, m = 4) is_date_xx(x)
make_date_xx(2017) make_date_xx(2017, 4) x <- make_date_xx(2017, m = 4) is_date_xx(x)
The arithmetic operations +
, -
as well as sequence generation with
seq()
are all supported for date_yq
and date_ym
objects. Other binary
arithmetic operators are disabled (see date_xx_arithmetic_disabled).
## S3 method for class 'date_xx' x + y ## S3 method for class 'date_xx' x - y
## S3 method for class 'date_xx' x + y ## S3 method for class 'date_xx' x - y
x |
|
y |
an integer |
q <- date_yq(2018, 1) q + 5 q - 1 seq(q, q + 5) m <- date_ym(2018, 12) m + 1 m - 13 seq(m - 1, m + 1)
q <- date_yq(2018, 1) q + 5 q - 1 seq(q, q + 5) m <- date_ym(2018, 12) m + 1 m - 13 seq(m - 1, m + 1)
This page lists operators that are disabled for date_yq
and date_ym
objects.
## S3 method for class 'date_xx' x * y ## S3 method for class 'date_xx' x / y ## S3 method for class 'date_xx' x ^ y ## S3 method for class 'date_xx' x %% y ## S3 method for class 'date_xx' x %/% y ## S3 method for class 'date_y' x %% y ## S3 method for class 'date_y' x %/% y
## S3 method for class 'date_xx' x * y ## S3 method for class 'date_xx' x / y ## S3 method for class 'date_xx' x ^ y ## S3 method for class 'date_xx' x %% y ## S3 method for class 'date_xx' x %/% y ## S3 method for class 'date_y' x %% y ## S3 method for class 'date_y' x %/% y
x |
|
y |
an integer |
date_xx_arithmetic, base::Arithmetic
date_*_breaks
does not return breaks, but a function that calculates
breaks. This is for compatibility with the breaks functions from scales
such as scales::pretty_breaks()
, and for ease of use with ggplot2.
date_yq_breaks(n = 6) date_ym_breaks(n = 6) date_yw_breaks(n = 6)
date_yq_breaks(n = 6) date_ym_breaks(n = 6) date_yw_breaks(n = 6)
n |
|
a function
that calculates a maximum of n
breaks for a date_xx
vector
x <- date_ym(2016, 1:12) date_ym_breaks()(x) date_ym_breaks(12)(x)
x <- date_ym(2016, 1:12) date_ym_breaks()(x) date_ym_breaks(12)(x)
date_xx Sequence Generation
## S3 method for class 'date_yw' seq(from, to, by = 1L, ...) ## S3 method for class 'date_yq' seq(from, to, by = 1L, ...) ## S3 method for class 'date_ym' seq(from, to, by = 1L, ...)
## S3 method for class 'date_yw' seq(from, to, by = 1L, ...) ## S3 method for class 'date_yq' seq(from, to, by = 1L, ...) ## S3 method for class 'date_ym' seq(from, to, by = 1L, ...)
from , to
|
the starting and (maximal) end value of the sequence. Must be
of the same class (i.e. both must be a |
by |
a positive |
... |
ignored |
an integer
vector with the same date_xx
subclass as from
/to
A simple data type for storing years. A date_y
object is just an
integer with an additional class attribute.
date_y(y) is_date_y(x) as_date_y(x)
date_y(y) is_date_y(x) as_date_y(x)
y |
year |
x |
any R object |
date_y
returns an object of type date_y
is_date_y
returns TRUE
or FALSE
depending on whether its
argument is of type date_y
or not.
as_date_m
attempts to coerce its argument to date_y
type
Other date_xx subclasses:
date_ym()
,
date_yq()
,
date_yw()
date_y(2013) as_date_y(2016)
date_y(2013) as_date_y(2016)
A simple data type for storing year-month dates in a human readable integer
format, e.g.: December 2012 is stored as 201212. Supports simple
arithmetic operations such as +
and -
as well formatting.
date_ym(y, m) is_date_ym(x) as_date_ym(x)
date_ym(y, m) is_date_ym(x) as_date_ym(x)
y |
year |
m |
month (optional) |
x |
any R object |
date_ym
returns an object of type date_ym
is_date_ym
returns TRUE
or FALSE
depending on whether its
argument is of type date_ym
or not.
as_date_ym
attempts to coerce its argument to date_ym
format.date_ym()
, seq.date_ym()
, date_xx_arithmetic()
Other date_xx subclasses:
date_yq()
,
date_yw()
,
date_y()
date_ym(2013, 12) as_date_ym(201612)
date_ym(2013, 12) as_date_ym(201612)
A simple data type for storing year-quarter dates in a human readable integer
format, e.g.: 3.Quarter of 2012 is stored as 20123. Supports simple
arithmetic operations such as +
and -
as well formatting.
date_yq(y, q) is_date_yq(x) as_date_yq(x)
date_yq(y, q) is_date_yq(x) as_date_yq(x)
y |
year |
q |
quarter (optional) |
x |
any R object |
date_yq
returns an object of type date_yq
is_date_yq
returns TRUE
or FALSE
depending on whether its
argument is of type date_yq
or not.
as_date_yq
attempts to coerce its argument to date_yq
format.date_yq()
, seq.date_yq()
, date_xx_arithmetic()
Other date_xx subclasses:
date_ym()
,
date_yw()
,
date_y()
date_yq(2013, 3) as_date_yq(20161)
date_yq(2013, 3) as_date_yq(20161)
A simple data type for storing year-isoweek dates in a human readable integer
format, e.g.: the 52nd isoweek of 2012 is stored as 201252. Supports simple
arithmetic operations such as +
and -
as well formatting.
date_yw(y, w) is_date_yw(x) as_date_yw(x)
date_yw(y, w) is_date_yw(x) as_date_yw(x)
y |
year |
w |
week (optional) |
x |
any R object |
date_yw
returns an object of type date_yw
is_date_yw
returns TRUE
or FALSE
depending on whether its
argument is of type date_yw
or not.
as_date_yw
attempts to coerce its argument to date_yw
format.date_yw()
, seq.date_yw()
, date_xx_arithmetic()
Other date_xx subclasses:
date_ym()
,
date_yq()
,
date_y()
date_yw(2013, 12) as_date_yw(201612)
date_yw(2013, 12) as_date_yw(201612)
first_of_yw()
is equivalent with first_of_isoweek()
and only included
for symmetry with first_of_yq()
and first_of_ym()
.
first_of_isoweek(x) ## Default S3 method: first_of_isoweek(x) last_of_isoweek(x) ## Default S3 method: last_of_isoweek(x) first_of_yw(x, w = NULL) last_of_yw(x, w = NULL)
first_of_isoweek(x) ## Default S3 method: first_of_isoweek(x) last_of_isoweek(x) ## Default S3 method: last_of_isoweek(x) first_of_yw(x, w = NULL) last_of_yw(x, w = NULL)
x |
Anything that can be coerced to a date with |
w |
Two integer (vectors).
|
a Date
first_of_isoweek("2016-06-04") last_of_isoweek("2016-06-04") first_of_yw(2016) first_of_yw(2016)
first_of_isoweek("2016-06-04") last_of_isoweek("2016-06-04") first_of_yw(2016) first_of_yw(2016)
Get First / Last Day of the First and Last Isoweek of a Year
first_of_isoyear(x) ## Default S3 method: first_of_isoyear(x) ## S3 method for class 'date_yw' first_of_isoyear(x) ## S3 method for class 'integer' first_of_isoyear(x) ## S3 method for class 'numeric' first_of_isoyear(x) last_of_isoyear(x) ## Default S3 method: last_of_isoyear(x) ## S3 method for class 'date_yw' last_of_isoyear(x) ## S3 method for class 'integer' last_of_isoyear(x) ## S3 method for class 'numeric' last_of_isoyear(x)
first_of_isoyear(x) ## Default S3 method: first_of_isoyear(x) ## S3 method for class 'date_yw' first_of_isoyear(x) ## S3 method for class 'integer' first_of_isoyear(x) ## S3 method for class 'numeric' first_of_isoyear(x) last_of_isoyear(x) ## Default S3 method: last_of_isoyear(x) ## S3 method for class 'date_yw' last_of_isoyear(x) ## S3 method for class 'integer' last_of_isoyear(x) ## S3 method for class 'numeric' last_of_isoyear(x)
x |
anything that can be coerced to a |
Get First / Last Day of a Month
Get First or Last Day of Month From Year and Month
first_of_month(x) ## Default S3 method: first_of_month(x) last_of_month(x) ## Default S3 method: last_of_month(x) first_of_ym(x, m = NULL) last_of_ym(x, m = NULL)
first_of_month(x) ## Default S3 method: first_of_month(x) last_of_month(x) ## Default S3 method: last_of_month(x) first_of_ym(x, m = NULL) last_of_ym(x, m = NULL)
x |
Anything that can be coerced to a date with |
m |
Two integer (vectors).
|
a Date
first_of_month("2016-06-04") last_of_month("2016-06-04") first_of_ym(2016, 1) first_of_ym(201601)
first_of_month("2016-06-04") last_of_month("2016-06-04") first_of_ym(2016, 1) first_of_ym(201601)
Get First / Last Day of a Quarter
first_of_quarter(x) ## Default S3 method: first_of_quarter(x) last_of_quarter(x) ## Default S3 method: last_of_quarter(x)
first_of_quarter(x) ## Default S3 method: first_of_quarter(x) last_of_quarter(x) ## Default S3 method: last_of_quarter(x)
x |
Anything that can be coerced to a date with |
a Date
first_of_quarter("2016-06-04") last_of_quarter("2016-06-04")
first_of_quarter("2016-06-04") last_of_quarter("2016-06-04")
Get First / Last Day of a Year
first_of_year(x) ## S3 method for class 'date_xx' first_of_year(x) ## S3 method for class 'integer' first_of_year(x) ## Default S3 method: first_of_year(x) ## S3 method for class 'numeric' first_of_year(x) last_of_year(x) ## S3 method for class 'date_xx' last_of_year(x) ## S3 method for class 'integer' last_of_year(x) ## Default S3 method: last_of_year(x) ## S3 method for class 'numeric' last_of_year(x)
first_of_year(x) ## S3 method for class 'date_xx' first_of_year(x) ## S3 method for class 'integer' first_of_year(x) ## Default S3 method: first_of_year(x) ## S3 method for class 'numeric' first_of_year(x) last_of_year(x) ## S3 method for class 'date_xx' last_of_year(x) ## S3 method for class 'integer' last_of_year(x) ## Default S3 method: last_of_year(x) ## S3 method for class 'numeric' last_of_year(x)
x |
Anything that can be coerced to a date with |
a Date
first_of_year("2016-06-04") last_of_year("2016-06-04")
first_of_year("2016-06-04") last_of_year("2016-06-04")
Get First or Last Day of Quarter From Year and Quarter
first_of_yq(x, q = NULL) last_of_yq(x, q = NULL)
first_of_yq(x, q = NULL) last_of_yq(x, q = NULL)
x |
Two integer (vectors).
|
q |
Two integer (vectors).
|
a Date
first_of_yq(2016, 1) first_of_yq(20161)
first_of_yq(2016, 1) first_of_yq(20161)
Format a date_xx
## S3 method for class 'date_y' format(x, format = "%Y", ...) ## S3 method for class 'date_yq' format( x, format = "%Y-Q%q", month_names = format(ISOdate(2000, 1:12, 1), "%B"), month_abb = format(ISOdate(2000, 1:12, 1), "%b"), ... ) ## S3 method for class 'date_ym' format( x, format = "%Y-M%m", month_names = format(ISOdate(2000, 1:12, 1), "%B"), month_abb = format(ISOdate(2000, 1:12, 1), "%b"), ... ) ## S3 method for class 'date_yw' format(x, format = "%Y-W%V", ...) format_yq_iso(x) format_yq_short(x) format_yq_shorter(x) format_ym_iso(x) format_ym_short(x) format_ym_shorter(x) format_yw_iso(x) format_yw_short(x) format_yw_shorter(x)
## S3 method for class 'date_y' format(x, format = "%Y", ...) ## S3 method for class 'date_yq' format( x, format = "%Y-Q%q", month_names = format(ISOdate(2000, 1:12, 1), "%B"), month_abb = format(ISOdate(2000, 1:12, 1), "%b"), ... ) ## S3 method for class 'date_ym' format( x, format = "%Y-M%m", month_names = format(ISOdate(2000, 1:12, 1), "%B"), month_abb = format(ISOdate(2000, 1:12, 1), "%b"), ... ) ## S3 method for class 'date_yw' format(x, format = "%Y-W%V", ...) format_yq_iso(x) format_yq_short(x) format_yq_shorter(x) format_ym_iso(x) format_ym_short(x) format_ym_shorter(x) format_yw_iso(x) format_yw_short(x) format_yw_shorter(x)
x |
any R object. |
||||||||||||
format |
A format that uses a subset of the same placeholders as
Not all placeholders are supported for all |
||||||||||||
... |
ignored |
||||||||||||
month_names , month_abb
|
a |
a character
vector
Format shorthand functions in the form of format_y*_[preset]()
directly
apply formatting presets to anything that can be coerced to a date_xx
.
This is notably handy as they can be used as a labeling function for
ggplot2 axes (see vignette("dint")
)
x <- date_ym(2018, c(1L, 10L, 3L, 6L, 4L, 5L, 7L, 12L, 2L, 9L, 8L, 11L)) fm <- "%Y-M%m: %B,%b" format( x, format = fm, month_names = month.name, # built-in R constant for English names month_abb = month.abb )
x <- date_ym(2018, c(1L, 10L, 3L, 6L, 4L, 5L, 7L, 12L, 2L, 9L, 8L, 11L)) fm <- "%Y-M%m: %B,%b" format( x, format = fm, month_names = month.name, # built-in R constant for English names month_abb = month.abb )
Coerce and Format to Year-Month Strings
format_ym(x, m = NULL, format = "%Y-M%m")
format_ym(x, m = NULL, format = "%Y-M%m")
x , m
|
Two integer (vectors).
|
||||||||||||
format |
A format that uses a subset of the same placeholders as
Not all placeholders are supported for all |
a character
vector
Format shorthand functions in the form of format_y*_[preset]()
directly
apply formatting presets to anything that can be coerced to a date_xx
.
This is notably handy as they can be used as a labeling function for
ggplot2 axes (see vignette("dint")
)
Other coerce and format functions:
format_yq()
,
format_yw()
format_ym(2015, 5) format_ym(201505, format = "short") format_ym(201505, format = "shorter")
format_ym(2015, 5) format_ym(201505, format = "short") format_ym(201505, format = "shorter")
Coerce and Format to Year-Quarter Strings
format_yq(x, q = NULL, format = "%Y-Q%q")
format_yq(x, q = NULL, format = "%Y-Q%q")
x , q
|
Two integer (vectors).
|
||||||||||||
format |
A format that uses a subset of the same placeholders as
Not all placeholders are supported for all |
a character
vector
Format shorthand functions in the form of format_y*_[preset]()
directly
apply formatting presets to anything that can be coerced to a date_xx
.
This is notably handy as they can be used as a labeling function for
ggplot2 axes (see vignette("dint")
)
Other coerce and format functions:
format_ym()
,
format_yw()
format_yq(2015, 1) format_yq(20151, format = "short") format_yq(20151, format = "shorter")
format_yq(2015, 1) format_yq(20151, format = "short") format_yq(20151, format = "shorter")
Coerce and Format to Year-Isoweek Strings
format_yw(x, w = NULL, format = "%Y-W%V")
format_yw(x, w = NULL, format = "%Y-W%V")
x , w
|
Two integer (vectors).
|
||||||||||||
format |
A format that uses a subset of the same placeholders as
Not all placeholders are supported for all |
a character
vector
Format shorthand functions in the form of format_y*_[preset]()
directly
apply formatting presets to anything that can be coerced to a date_xx
.
This is notably handy as they can be used as a labeling function for
ggplot2 axes (see vignette("dint")
)
Other coerce and format functions:
format_ym()
,
format_yq()
format_yw(2015, 5) format_yw(201505, format = "%Y.%V") format_yw(as_date_yw(201505), format = "%y.%V")
format_yw(2015, 5) format_yw(201505, format = "%Y.%V") format_yw(as_date_yw(201505), format = "%y.%V")
Get Year, Quarter, Month or Isoweek
get_year(x) get_quarter(x) get_month(x) get_isoweek(x) get_isoyear(x)
get_year(x) get_quarter(x) get_month(x) get_isoweek(x) get_isoyear(x)
x |
a date_xx or any R object that can be coerced to |
If you use lubridate in addition to dint,
you can also use lubridate::year()
, lubridate::month()
and
lubridate::quarter()
with dint objects.
an integer
vector.
lubridate::year()
,
lubridate::month()
,
lubridate::quarter()
x <- date_yq(2016, 2) get_year(x) ## Not run: library(lubridate) year(x) ## End(Not run) x <- date_yq(2016, 2) get_quarter(x) ## Not run: library(lubridate) quarter(x) ## End(Not run) x <- date_yq(2016, 2) get_month(x) ## Not run: library(lubridate) month(x) ## End(Not run) x <- date_yw(2016, 2) get_isoweek(x) get_isoyear(as.Date("2018-01-01")) get_isoyear(as.Date("2016-01-01"))
x <- date_yq(2016, 2) get_year(x) ## Not run: library(lubridate) year(x) ## End(Not run) x <- date_yq(2016, 2) get_quarter(x) ## Not run: library(lubridate) quarter(x) ## End(Not run) x <- date_yq(2016, 2) get_month(x) ## Not run: library(lubridate) month(x) ## End(Not run) x <- date_yw(2016, 2) get_isoweek(x) get_isoyear(as.Date("2018-01-01")) get_isoyear(as.Date("2016-01-01"))
Increment date_xx objects
increment(x, inc = 1) ## S3 method for class 'date_yq' increment(x, inc) ## S3 method for class 'date_ym' increment(x, inc) ## S3 method for class 'date_yw' increment(x, inc) ## S3 method for class 'date_y' increment(x, inc)
increment(x, inc = 1) ## S3 method for class 'date_yq' increment(x, inc) ## S3 method for class 'date_ym' increment(x, inc) ## S3 method for class 'date_yw' increment(x, inc) ## S3 method for class 'date_y' increment(x, inc)
x |
object to increment |
inc |
Value by which to increment (usually integer) |
An object of the same type as x
increment by inc
is_first_of_quarter()
, is_last_of_quarter()
, is_first_of_year()
and
is_last_of_year()
check whether a Date
is the first or respectively the
last day of a quarter/year. is_quarter_bounds()
and is_year_bounds
checks
whether two Date
vectors mark the bounds of (the same) quarters
is_quarter_bounds(first, last) is_first_of_quarter(x) is_last_of_quarter(x) is_year_bounds(first, last) is_first_of_year(x) is_last_of_year(x) is_Date(x) is_POSIXlt(x)
is_quarter_bounds(first, last) is_first_of_quarter(x) is_last_of_quarter(x) is_year_bounds(first, last) is_first_of_year(x) is_last_of_year(x) is_Date(x) is_POSIXlt(x)
x , first , last
|
|
a logical
vector
x <- as.Date(c("2018-01-01", "2018-03-31", "2018-02-14")) is_first_of_year(x) is_first_of_quarter(x) is_last_of_quarter(x) is_quarter_bounds(x[[1]], x[[2]]) is_quarter_bounds(x[[2]], x[[3]])
x <- as.Date(c("2018-01-01", "2018-03-31", "2018-02-14")) is_first_of_year(x) is_first_of_quarter(x) is_last_of_quarter(x) is_quarter_bounds(x[[1]], x[[2]]) is_quarter_bounds(x[[2]], x[[3]])
Comparison Operators for date_xx
## S3 method for class 'date_xx' Ops(e1, e2)
## S3 method for class 'date_xx' Ops(e1, e2)
e1 , e2
|
Objects with the same |
a logical
scalar
date_yq(2015, 1) < date_yq(2015, 2) # comparison with integers is ok date_yq(2015, 1) < 20152 # but two different date_xx cannot be compared try(date_yq(2015, 1) < date_ym(2015, 2))
date_yq(2015, 1) < date_yq(2015, 2) # comparison with integers is ok date_yq(2015, 1) < 20152 # but two different date_xx cannot be compared try(date_yq(2015, 1) < date_ym(2015, 2))
Print a date_xx Object
## S3 method for class 'date_xx' print(x, ...)
## S3 method for class 'date_xx' print(x, ...)
x |
A date_xx object |
... |
passed on to |
x
(invisibly)
Replicate Elements of date_xx Vectors
## S3 method for class 'date_xx' rep(x, ...)
## S3 method for class 'date_xx' rep(x, ...)
x |
a date_xx |
... |
passed on to |
a vector of the same date_xx
subclass as x
Rounds a date_xx
to the first unit of the current year, or the first
unit of the next year.
## S3 method for class 'date_yq' round(x, digits = NULL) ## S3 method for class 'date_ym' round(x, digits = NULL) ## S3 method for class 'date_yw' round(x, digits = NULL) ## S3 method for class 'date_xx' ceiling(x) ## S3 method for class 'date_xx' floor(x)
## S3 method for class 'date_yq' round(x, digits = NULL) ## S3 method for class 'date_ym' round(x, digits = NULL) ## S3 method for class 'date_yw' round(x, digits = NULL) ## S3 method for class 'date_xx' ceiling(x) ## S3 method for class 'date_xx' floor(x)
x |
any |
digits |
ignored, only there for compatibility with |
a date_xx
of the same subclass as x
round(date_yq(2018, 2)) round(date_yq(2018, 3)) round(date_ym(2018, 6)) round(date_ym(2018, 7)) round(date_yw(2018, 26)) round(date_yw(2018, 27))
round(date_yq(2018, 2)) round(date_yq(2018, 3)) round(date_ym(2018, 6)) round(date_ym(2018, 7)) round(date_yw(2018, 26)) round(date_yw(2018, 27))
The scale_*_date_**
functions provide nice defaults for plotting
the appropriate date_xx subclass, but come with a limited number of
configuration options. If you require more finetuning, you can convert
date_xx vectors with as.Date()
and use ggplot2::scale_x_date()
.
scale_x_date_yq( name = "Quarter", breaks = date_yq_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_yq( name = "Quarter", breaks = date_yq_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" ) scale_x_date_ym( name = "Month", breaks = date_ym_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_ym( name = "Month", breaks = date_ym_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" ) scale_x_date_yw( name = "Week", breaks = date_yw_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_yw( name = "Week", breaks = date_yw_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" )
scale_x_date_yq( name = "Quarter", breaks = date_yq_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_yq( name = "Quarter", breaks = date_yq_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" ) scale_x_date_ym( name = "Month", breaks = date_ym_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_ym( name = "Month", breaks = date_ym_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" ) scale_x_date_yw( name = "Week", breaks = date_yw_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "bottom" ) scale_y_date_yw( name = "Week", breaks = date_yw_breaks(), labels = ggplot2::waiver(), limits = NULL, position = "left" )
name |
The name of the scale. Used as the axis or legend title. If
|
breaks |
One of:
|
labels |
One of:
|
limits |
One of:
|
position |
For position scales, The position of the axis.
|
if (require("ggplot2", quietly = TRUE)){ dd <- data.frame(date = seq(date_yq(2016, 1), date_yq(2018, 1)), V1 = 1:9) p <- ggplot(dd, aes(x = date, y = V1)) + geom_point() p # automatically uses the proper scale p + scale_x_date_yq("quarters with default spacing") p + scale_x_date_yq(breaks = date_yq_breaks(3)) # Different ways to specify breaks and labels p <- ggplot( data.frame(date = seq(date_yq(2012, 4), date_yq(2018, 4)), V1 = 1:25), aes(x = date, y = V1) ) + geom_point() p + scale_x_date_yq(labels = waiver()) + ggtitle("auto Labels") p + scale_x_date_yq(labels = NULL) + ggtitle("no Labels") p + scale_x_date_yq(labels = LETTERS[1:4]) + ggtitle("manual Labels") p + scale_x_date_yq(labels = format_yq_iso) + ggtitle("function Labels") p + scale_x_date_yq(breaks = waiver()) + ggtitle("auto breaks") p + scale_x_date_yq(breaks = NULL) + ggtitle("no breaks") p + scale_x_date_yq(breaks = date_yq(2013, 2:3) ) + ggtitle("manual breaks") p + scale_x_date_yq(breaks = date_yq_breaks(1) ) + ggtitle("function breaks") }
if (require("ggplot2", quietly = TRUE)){ dd <- data.frame(date = seq(date_yq(2016, 1), date_yq(2018, 1)), V1 = 1:9) p <- ggplot(dd, aes(x = date, y = V1)) + geom_point() p # automatically uses the proper scale p + scale_x_date_yq("quarters with default spacing") p + scale_x_date_yq(breaks = date_yq_breaks(3)) # Different ways to specify breaks and labels p <- ggplot( data.frame(date = seq(date_yq(2012, 4), date_yq(2018, 4)), V1 = 1:25), aes(x = date, y = V1) ) + geom_point() p + scale_x_date_yq(labels = waiver()) + ggtitle("auto Labels") p + scale_x_date_yq(labels = NULL) + ggtitle("no Labels") p + scale_x_date_yq(labels = LETTERS[1:4]) + ggtitle("manual Labels") p + scale_x_date_yq(labels = format_yq_iso) + ggtitle("function Labels") p + scale_x_date_yq(breaks = waiver()) + ggtitle("auto breaks") p + scale_x_date_yq(breaks = NULL) + ggtitle("no breaks") p + scale_x_date_yq(breaks = date_yq(2013, 2:3) ) + ggtitle("manual breaks") p + scale_x_date_yq(breaks = date_yq_breaks(1) ) + ggtitle("function breaks") }
Maxima and Minima for date_xx
## S3 method for class 'date_xx' Summary(..., na.rm)
## S3 method for class 'date_xx' Summary(..., na.rm)
... |
|
na.rm |
logical: should missing values be removed? |
for min()
and max()
a scalar of the same date_xx
subclass as it's
input, for range a vector of length 2
min(date_yq(2014, 1), date_yq(2014, 2)) # raises an error try(min(date_yq(2014, 1), date_ym(2014, 2)))
min(date_yq(2014, 1), date_yq(2014, 2)) # raises an error try(min(date_yq(2014, 1), date_ym(2014, 2)))
See lubridate::year()
and lubridate::month()
year.date_xx(x) month.date_xx(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) isoweek.date_xx(x)
year.date_xx(x) month.date_xx(x, label = FALSE, abbr = TRUE, locale = Sys.getlocale("LC_TIME")) isoweek.date_xx(x)
x |
a date_xx or any R object that can be coerced to |
label |
logical. TRUE will display the month as a character string such as "January." FALSE will display the month as a number. |
abbr |
logical. FALSE will display the month as a character string label, such as "January". TRUE will display an abbreviated version of the label, such as "Jan". abbr is disregarded if label = FALSE. |
locale |
for month, locale to use for month names. Default to current locale. |
## Not run: library(lubridate) month(x) month(x, label = TRUE) ## End(Not run) ## Not run: library(lubridate) isoweek(x) ## End(Not run)
## Not run: library(lubridate) month(x) month(x, label = TRUE) ## End(Not run) ## Not run: library(lubridate) isoweek(x) ## End(Not run)
These are generic parsers for year/quarter/month formats that work with
nearly all possible year/quarter formats. The only prerequisite is that
x
contains a 4-digit-year and a 1-digit-quarter or 2-digit-month and no
additional numbers.
yq(x, quiet = FALSE) qy(x, quiet = FALSE) ym(x, quiet = FALSE) my(x, quiet = FALSE)
yq(x, quiet = FALSE) qy(x, quiet = FALSE) ym(x, quiet = FALSE) my(x, quiet = FALSE)
x |
a |
quiet |
a |
a date_yq
or date_ym
vector
yq("2018 1") qy("1st Quarter 2019") #' # Works even for filenames, as long as they contain no additional numbers yq("business_report-2018_1.pdf") my("business_report-082018.pdf")
yq("2018 1") qy("1st Quarter 2019") #' # Works even for filenames, as long as they contain no additional numbers yq("business_report-2018_1.pdf") my("business_report-082018.pdf")