
xts
is used to create an xts
object from raw data inputs.
xts(x, order.by = index(x),frequency = NULL, ...)is.xts(x)
order.by
. See details.xts
. As it inherits and extends
the zoo class, all zoo methods remain valid. Additional
attributes may be assigned and extracted via
xtsAttributes
.xts
object extends the S3 class zoo
from the package of the
same name.The first difference in this extension provides for a requirement
that the index values not only be unique and ordered, but also must be of
a time-based class. Currently acceptable classes include:
The second difference is that the object may now carry additional attributes that may be desired in individual time-series handling. This includes the ability to augment the objects data with meta-data otherwise not cleanly attachable to a standard zoo object.
Examples of usage from finance may include the addition of data for keeping track of sources, last-update times, financial instrument descriptions or details, etc.
The idea behind xts
is to offer the user the ability to utilize
a standard zoo object, while providing an mechanism to customize
the object's meta-data, as well as create custom methods to handle
the object in a manner required by the user.
Most, if not all, standard zoo methods will work without change. Where
changes where required (e.g. to return an xts object) new methods have been
written. At present these modified methods include "["
, coredata
,
[add more here]
Subsetting via "[" methods offers the ability to specify dates by
range, if they are enclosed in quotes. The style borrows from
python by creating ranges with a double colon "::" operator. Each side of
the operator may be left blank, which would then default to the beginning
and end of the data, respectively. To specify a subset of times,
it is only required that the time specified be in standard ISO format, with
some form of separation between the elements. The time must be
Users may also extend the xts
class to new classes to
allow for method overloading.
Additional benefits derive from the use of as.xts
and
reclass
, which allow for lossless two-way conversion
between common R time-series classes and the xts
object
structure. See those functions for more detail.
as.xts
, reclass
,
xtsAttributes
library(tseries)
yhoo <- get.hist.quote("YHOO")
sample.xts <- xts(yhoo,order.by=index(yhoo),descr="my ts object")
class(sample.xts)
str(sample.xts)
head(sample.xts) # attribute 'descr' hidden from view
attr(sample.xts,'descr')
sample.xts['2007'] # all of 2007
sample.xts['2007-03::'] # March 2007 to the end of the data set
sample.xts['2007-03::2007'] # March 2007 to the end of 2007
sample.xts['::'] # the whole data set
sample.xts['::2007'] # the beginning of the data through 2007
sample.xts['2007-01-03'] # just the 3rd of January 2007
Run the code above in your browser using DataLab