as.xts: Convert Object To And From Class xts

as.xtsR Documentation

Convert Object To And From Class xts


Conversion functions to coerce data objects of arbitrary classes to class xts and back, without losing any attributes of the original format.


as.xts(x, ...)


try.xts(x, ..., error = TRUE)
reclass(x,, error = FALSE, ...)



data object to convert. See details for supported types

xts object whose attributes will be passed to x


error handling option. See Details.


additional parameters or attributes


A simple and reliable way to convert many different objects into a uniform format for use within R.

It is possible with a call to as.xts to convert objects of class timeSeries, ts, irts, matrix, data.frame, and zoo.

xtsible safely checks whether an object can be converted to an xts object; returning TRUE on success and FALSE otherwise.

The help file as.xts.methods lists all available xts methods and arguments specific to each coercible type.

Additional name=value pairs may be passed to the function to be added to the new object. A special print.xts method will assure that the attributes are hidden from view, but will be available via R's standard attr function, as well as the xtsAttributes function.

The returned object will preserve all relevant attribute/slot data within itself, allowing for temporary conversion to use zoo and xts compatible methods. A call to reclass returns the object to its original class, with all original attributes intact - unless otherwise changed.

It should be obvious, but any attributes added via the ... argument will not be carried back to the original data object, as there would be no available storage slot/attribute.

Reclass is designed for top-level use, where it is desirable to have the object returned from an arbitrary function in the same class as the object passed in. Most functions within R are not designed to return objects matching the original object's class. While this tool is highly experimental at present, it attempts to handle conversion and reconversion transparently. The caveats are that the original object must be coercible to xts, the returned object must be of the same row length as the original object, and that the object to reconvert to is the first argument to the function being wrapped.

try.xts and reclass are functions that enable external developers access to the reclassing tools within xts to help speed development of time-aware functions, as well as provide a more robust and seemless end-user experience, regardless of the end-user's choice of data-classes.

The error argument to try.xts accepts a logical value, indicating where an error should be thrown, a character string allowing for custom error messages to be displayed, or a function of the form f(x, ...), to be called upon construction error.

See the accompanying vignette for more details on the above usage and the package in general.


An S3 object of class xts.

In the case of Reclass and reclass, the object returned will be of the original class as identified by CLASS.


Jeffrey A. Ryan

See Also


xts documentation built on April 17, 2023, 1:07 a.m.