Last data update: 2014.03.03

R: Round times.
round.POSIXltR Documentation

Round times.

Description

There are round methods in base for objects of DateTimeClasses. However, they can only round to full second, minutes, hours, and days. These functions offer some more options.

Usage

## S3 method for class 'POSIXlt'
round(x, digits = c("mins", "5mins", "10mins", "15mins", "quarter hours", 
"30mins", "half hours", "hours"))

## S3 method for class 'POSIXct'
round(x, ...)

Arguments

x

A DateTimeClasses object to be rounded. Needs to carry a timezone specification.

digits

Either a character string specifying the time units to round to (see choices above) or a numeric specifying the minutes to round to. To go to seconds just use values < 1, to go beyond the hour just use values > 60.

...

Further arguments to methods.

Value

A POSIXct object with rounded, not truncated date times.

Author(s)

Gerald Jurasinski, gerald.jurasinski@uni-rostock.de,

borrowing heavily from https://stat.ethz.ch/pipermail/r-help/2012-June/315336.html

Examples

# Current time in GMT and as class "POSIXlt"
zlt <- as.POSIXlt(Sys.time(), "GMT") 

# Same time as class POSIXct
zct <- as.POSIXct(zlt)

# round to minute
round(zct)

# round to half hour
round(zct, "30mins")
round(zct, "half hour")
round(zct, 30)

# round to 20 minutes
round(zlt, 20)

# round to 30 seconds
round(zlt, 0.5)

Results