| time_round | R Documentation |
A family of helpers to round date/time objects to a specified time granule
such as second, minute, hour, or day. These functions preserve the input
time class, as rounded by the attributes of the granule.
time_round(x, granule, ...)
time_ceiling(x, granule, ...)
time_floor(x, granule, ...)
x |
A date/time object to be rounded. Accepted types include Date, POSIXct, POSIXlt and other objects that inherit from POSIXt. The returned object will be of the same class as the input. |
granule |
A time granule (or object coercible to a time granule, e.g. "day"). |
... |
Additional arguments passed to specific implementations. |
An object of the same class as x with its time components adjusted to the requested granule.
base::round, lubridate::round_date
# Round POSIXct to the nearest minute (preserving tz)
t <- as.POSIXct("2020-01-01 12:34:56", tz = "UTC")
time_round(t, granule = cal_gregorian$minute(1L))
# Floor to the nearest hour
time_floor(t, granule = cal_gregorian$hour(1L))
# Ceiling a Date (treated as midnight-of-day rounding)
d <- as.Date("2020-01-01")
time_ceiling(d, granule = cal_gregorian$month(1L))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.