date-and-date-time-rounding | R Documentation |
date_floor()
rounds a date or date-time down to a multiple of
the specified precision
.
date_ceiling()
rounds a date or date-time up to a multiple of
the specified precision
.
date_round()
rounds up or down depending on what is closer,
rounding up on ties.
There are separate help pages for rounding dates and date-times:
dates (Date)
date-times (POSIXct/POSIXlt)
These functions round the underlying duration itself, relative to an
origin
. For example, rounding to 15 hours will construct groups of
15 hours, starting from origin
, which defaults to a naive time of
1970-01-01 00:00:00.
If you want to group by components, such as "day of the month", see
date_group()
.
date_floor(x, precision, ..., n = 1L, origin = NULL)
date_ceiling(x, precision, ..., n = 1L, origin = NULL)
date_round(x, precision, ..., n = 1L, origin = NULL)
x |
A date or date-time vector. |
precision |
A precision. Allowed precisions are dependent on the input used. |
... |
These dots are for future extensions and must be empty. |
n |
A single positive integer specifying a multiple of |
origin |
An origin to start counting from. The default |
x
rounded to the specified precision
.
# See the type specific documentation for more examples
x <- as.Date("2019-03-31") + 0:5
x
# Flooring by 2 days, note that this is not tied to the current month,
# and instead counts from the specified `origin`.
date_floor(x, "day", n = 2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.