posixt-rounding | R Documentation |
These are POSIXct/POSIXlt methods for the rounding generics.
date_floor()
rounds a date-time down to a multiple of
the specified precision
.
date_ceiling()
rounds a 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.
You can group by irregular periods such as "month"
or "year"
by using
date_group()
.
## S3 method for class 'POSIXt'
date_floor(
x,
precision,
...,
n = 1L,
origin = NULL,
nonexistent = NULL,
ambiguous = x
)
## S3 method for class 'POSIXt'
date_ceiling(
x,
precision,
...,
n = 1L,
origin = NULL,
nonexistent = NULL,
ambiguous = x
)
## S3 method for class 'POSIXt'
date_round(
x,
precision,
...,
n = 1L,
origin = NULL,
nonexistent = NULL,
ambiguous = x
)
x |
A date-time vector. |
precision |
One of:
|
... |
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.
If |
nonexistent |
One of the following nonexistent time resolution strategies, allowed to be either length 1, or the same length as the input:
Using either If If |
ambiguous |
One of the following ambiguous time resolution strategies, allowed to be either length 1, or the same length as the input:
Alternatively, Finally, If If |
When rounding by "week"
, remember that the origin
determines the "week
start". By default, 1970-01-01 is the implicit origin, which is a
Thursday. If you would like to round by weeks with a different week start,
just supply an origin on the weekday you are interested in.
x
rounded to the specified precision
.
x <- as.POSIXct("2019-03-31", "America/New_York")
x <- add_days(x, 0:5)
# Flooring by 2 days, note that this is not tied to the current month,
# and instead counts from the specified `origin`, so groups can cross
# the month boundary
date_floor(x, "day", n = 2)
# Compare to `date_group()`, which groups by the day of the month
date_group(x, "day", n = 2)
# Note that daylight saving time gaps can throw off rounding
x <- as.POSIXct("1970-04-26 01:59:59", "America/New_York") + c(0, 1)
x
# Rounding is done in naive-time, which means that rounding by 2 hours
# will attempt to generate a time of 1970-04-26 02:00:00, which doesn't
# exist in this time zone
try(date_floor(x, "hour", n = 2))
# You can handle this by specifying a nonexistent time resolution strategy
date_floor(x, "hour", n = 2, nonexistent = "roll-forward")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.