tdiff: Time Differences

tdiffR Documentation

Time Differences

Description

Objects of tdiff class represent time differences and are similar to difftime objects. tdiff objects are created by subtracting two time indices (of types other than "i" and "n") or via calls to as.tdiff method. An alternative way of constructing tdiff objects is to call years, qrtrs, mnths, weeks, days, hours, mins, and secs convenience functions.

The following units (argument unit) are supported:

"y" ("year", "years")

differences in years,

"q" ("quarter", "quarters")

differences in quarters,

"m" ("month", "months")

differences in months,

"w" ("week", "weeks")

differences in weeks,

"d" ("day", "days")

differences in days,

"h" ("hour", "hours")

differences in hours,

"min" ("mins", "minute", "minutes")

differences in minutes,

"s" ("secs", "second", "seconds")

differences in seconds.

Standard methods for vectors and conversion from / to numeric and character vectors are implemented for this class.

Usage

is.tdiff(x)

as.tdiff(x, ...)

## S3 method for class 'numeric'
as.tdiff(x, unit, ...)

## S3 method for class 'character'
as.tdiff(x, ...)

## S3 method for class 'difftime'
as.tdiff(x, ...)

years(x)

qrtrs(x)

mnths(x)

weeks(x)

days(x)

hours(x)

mins(x)

secs(x)

## S3 method for class 'tdiff'
as.character(x, ...)

## S3 method for class 'tdiff'
format(x, ...)

## S3 method for class 'tdiff'
as.data.frame(x, ...)

## S3 method for class 'tdiff'
x[i]

## S3 replacement method for class 'tdiff'
x[i] <- value

## S3 method for class 'tdiff'
x[[i]]

## S3 replacement method for class 'tdiff'
x[[i]] <- value

## S3 method for class 'tdiff'
rep(x, ...)

## S3 method for class 'tdiff'
c(...)

## S3 method for class 'tdiff'
Math(x, ...)

## S3 method for class 'tdiff'
Summary(..., na.rm = FALSE)

## S3 method for class 'tdiff'
mean(x, na.rm = FALSE, ...)

## S3 method for class 'tdiff'
unique(x, ...)

## S3 method for class 'tdiff'
print(x, ...)

## S3 method for class 'tdiff'
summary(object, ...)

Arguments

x

a numeric vector or any R object coercible to tdiff, for as.tdiff and years, qrtrs, etc.; an object of tdiff class for methods.

...

further arguments passed to or from other methods.

unit

a character string, name of the time unit, see Details.

i

an integer vector of indices or a logical vector indicating selection.

value

replacement value.

na.rm

a logical value indicating whether missing values should be removed.

object

an object of tdiff class.

Details

tdiff objects are implemented as vectors of integers (for differences in years, quarters, months, weeks, and days) or vectors of doubles (for time differences). Time differences are internally represented in seconds but when printing the actual time unit (hour, minute, second) is automatically inferred and used.

Valid ranges for tdiff values depend on unit. These are defined by differences of the maximal and minimal valid time indices of the type corresponding to the time unit.

Value

as.tdiff as well as convenience functions years, qrtrs, etc., return objects of tdiff class.

is.tdiff returns a logical value.

In general, methods for tdiff return objects of tdiff class.

as.character and format return character vectors.

as.data.frame returns a data frame with a single column and the number of rows equal to the length of the argument.

print returns its argument invisibly and is used for its side effect.

summary returns an object of class c("summaryDefault", "table").

Note

Since as.difftime is not implemented in base as an S3 generic, conversion from tdiff to difftime is not provided.

See Also

Ops for operations on time indices and time differences.

Examples

# how many days have passed since Jan 1, 2000?
today() - as.date("2000-01-01")
# how many months have passed since Sep 2008?
as.month(today()) - as.month("2008-09")
# create time differences in quarters
as.tdiff(-2:2, "q")
# same
(x <- qrtrs(-2:2))
# add to today
today() + x


tind documentation built on Dec. 28, 2025, 1:06 a.m.