tclass: Get or Replace the Class of an xts Object's Index

View source: R/tclass.R

tclassR Documentation

Get or Replace the Class of an xts Object's Index

Description

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 replacement method:
tclass(x) <- value

indexClass(x)

indexClass(x) <- value

## S3 replacement 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).

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".

Value

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

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.

Author(s)

Jeffrey A. Ryan

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()

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
head(.index(y))
tclass(y) <- "Date"
head(y)          # the index prints as a Date
head(.index(y))  # the internal index is truncated


joshuaulrich/xts documentation built on Nov. 17, 2024, 6:37 a.m.