Learn R Programming

tind (version 0.2.4)

t_unit: Get Time Unit

Description

t_unit method returns time unit of time difference object as a character value, either in short form (most often single letter, code used internally) or long form (name).

units method for tdiff objects is equivalent to t_unit with x argument only.

Usage

t_unit(x, long = TRUE, valid = FALSE)

# S3 method for tdiff t_unit(x, long = TRUE, valid = FALSE)

# S3 method for difftime t_unit(x, long = TRUE, valid = FALSE)

# S3 method for tdiff units(x)

Value

A character value.

Arguments

x

an object of tdiff class or an object coercible to it.

long

a logical value, if FALSE, internal code of time unit is returned; if TRUE, long (human-readable) name is returned (TRUE by default).

valid

a logical value, if TRUE, syntactically valid names will be returned (FALSE by default). Currently, has no impact as all unit names are syntactically valid.

Details

Time differences (for example differences between date-time indices) are internally represented as number of seconds. However, the returned time unit is automatically determined based on the resolution of the argument. If all time differences are full hours or full minutes, appropriate unit is returned, see Examples. For time differences that contain zeros and missing values only, returned unit is a second.

See Also

tdiff, ti_type.

Examples

Run this code
(x <- as.tdiff(1, "y"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "q"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "m"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "w"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "d"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "h"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "min"))
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(1, "s"))
t_unit(x)
t_unit(x, FALSE)
# automatic unit determination
(x <- as.tdiff(600, "s")) # ten minutes
t_unit(x)
t_unit(x, FALSE)
(x <- as.tdiff(7200, "s")) # two hours
t_unit(x)
t_unit(x, FALSE)

Run the code above in your browser using DataLab