Extracting/Replacing the Class of an xts Index

Generic functions to extract, replace, and format the class of the index of an xts object.

# S3 method for xts
index(x, ...)
# S3 method for xts
index(x) <- value

.index(x, ...) .index(x) <- value

indexClass(x) indexClass(x) <- value

tclass(x) tclass(x) <- value

indexFormat(x) indexFormat(x) <- value


# time component extraction/conversion


.indexday(x) .indexmday(x) .indexwday(x) .indexweek(x) .indexmon(x) .indexyday(x) .indexyear(x)

.indexhour(x) .indexmin(x) .indexsec(x)


xts object


desired new class or format. See details

additional arguments (unused)


The main accessor methods to an xts object's index is via the index and index<- replacement method. The structure of the index internally is now a numeric value corresponding to seconds since the epoch (POSIXct converted to numeric). This change allows for near native-speed matrix subsetting, as well as nearly instantaneous speed subsets by time.

A call to index translates to the desired class on-the-fly. The desired index class is stored as an attribute within the xts object. Upon a standard index call, this is used to convert the numeric value to the desired class.

It is possible to view and set the class of the time-index of a given xts object via the indexClass function.

To retrieve the raw numeric data a new accessor function (and replacement) has been added .index. This is primarily for internal use, but may be useful for end-users.

.indexXXX functions are useful to extract time components of the underlying time index. The ‘indexClass’ is virtual, and as such suitable conversions are made depending on the component requested.

The specified value for indexClass<- must be a character string containing one of the following: Date, POSIXct, chron, yearmon, yearqtr or timeDate.

indexFormat only manages the manner in which the object is displayed via print (also called automatically when the object is returned) and in conversion to other classes such as matrix. The valid values for indexFormat are the same for format.POSIXct, as this is the function that does the conversion internally.

convertIndex returns a modified xts object, and does not alter the original.

Changing the index type may alter the behavior of xts functions expecting a different index, as well as the functionality of additional methods. Use with caution.

  • indexClass
  • tclass
  • indexFormat
  • convertIndex
  • indexClass<-
  • tclass<-
  • indexFormat<-
  • index.xts
  • index<-.xts
  • .indexhour
  • .indexsec
  • .indexmin
  • .indexyear
  • .indexymon
  • .indexyday
  • .indexday
  • .indexDate
  • .indexmday
  • .indexwday
  • .indexweek
  • .indexmon
  • .index
  • .index<-
library(xts) # NOT RUN { x <- timeBasedSeq('2010-01-01/2010-01-02 12:00') x <- xts(1:length(x), x) # all obs. in the first 6 and last 3 minutes of the # 8th and 15th hours on each day x[.indexhour(x) %in% c(8,15) & .indexmin(x) %in% c(0:5,57:59)] # change the index format indexFormat(x) <- "%Y-%b-%d %H:%M:%OS3" head(x) # }
Documentation reproduced from package xts, version 0.9-7, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.