Skip to contents

Generic functions to get or replace the class of an xts object's index.

Usage

tclass(x, ...)

# Default S3 method
tclass(x, ...)

# S3 method for class 'xts'
tclass(x, ...)

tclass(x) <- value

# Default S3 method
tclass(x) <- value

indexClass(x)

indexClass(x) <- value

# S3 method for class 'xts'
tclass(x) <- value

Arguments

x

An xts object.

...

Arguments passed to other methods.

value

The new index class (see Details for valid values).

Value

A vector containing the class of the object's index.

Details

Internally, an xts object's index is a numeric value corresponding to seconds since the epoch in the UTC timezone. The index class is stored as the tclass attribute on the internal index. This is used to convert the internal index values to the desired class when the index function is called.

The tclass function retrieves the class of the internal index, and the tclass<- function sets it. The specified value for tclass<- must be one of the following character strings: "Date", "POSIXct", "chron", "yearmon", "yearqtr", or "timeDate".

Note

Both indexClass and indexClass<- are deprecated in favor of tclass and tclass<-, respectively.

Replacing the tclass can potentially change the values of the internal index. For example, changing the 'tclass' from POSIXct to Date will truncate the POSIXct value and convert the timezone to UTC (since the Date class doesn't have a timezone). See the examples.

See also

index() has more information on the xts index, tformat() details how the index values are formatted when printed, and tzone() has more information about the index timezone settings.

The following help pages describe the characteristics of the valid index classes: POSIXct(), Date(), chron(), yearmon(), yearqtr(), timeDate()

Author

Jeffrey A. Ryan

Examples


x <- timeBasedSeq('2010-01-01/2010-01-02 12:00')
x <- xts(seq_along(x), x)

y <- timeBasedSeq('2010-01-01/2010-01-03 12:00/H')
y <- xts(seq_along(y), y, tzone = "America/New_York")

# Changing the tclass *changes* the internal index values
head(y)          # the index has times
#> Warning: object timezone ('America/New_York') is different from system timezone ('')
#>                     [,1]
#> 2009-12-31 19:00:00    1
#> 2009-12-31 20:00:00    2
#> 2009-12-31 21:00:00    3
#> 2009-12-31 22:00:00    4
#> 2009-12-31 23:00:00    5
#> 2010-01-01 00:00:00    6
head(.index(y))
#> [1] 1262304000 1262307600 1262311200 1262314800 1262318400 1262322000
tclass(y) <- "Date"
head(y)          # the index prints as a Date
#>            [,1]
#> 2010-01-01    1
#> 2010-01-01    2
#> 2010-01-01    3
#> 2010-01-01    4
#> 2010-01-01    5
#> 2010-01-01    6
head(.index(y))  # the internal index is truncated
#> [1] 1262304000 1262304000 1262304000 1262304000 1262304000 1262304000