Using the xts package

The xts package provides an extensible time series class that provides uniform handling of many R time series classes by extending zoo.

Installation

The xts package can be installed from CRAN as follows

install.packages("xts")

Creating data objects: as.xts and xts

Existing time-series data can be converted to an xts object with as,xts. It is possible with a call to as.xts to convert objects of class timeSeries, ts, irts, fts, its, matrix, data.frame, and zoo.

library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
data(sample_matrix)
class(sample_matrix)
## [1] "matrix"
matrix_xts <- as.xts(sample_matrix, dateFormat='Date')
str(matrix_xts)
## An 'xts' object on 2007-01-02/2007-06-30 containing:
##   Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:4] "Open" "High" "Low" "Close"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
df_xts <- as.xts(as.data.frame(sample_matrix),
                 important='very important info!')
str(df_xts)
## An 'xts' object on 2007-01-02/2007-06-30 containing:
##   Data: num [1:180, 1:4] 50 50.2 50.4 50.4 50.2 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:4] "Open" "High" "Low" "Close"
##   Indexed by objects of class: [POSIXct,POSIXt] TZ: 
##   xts Attributes:  
## List of 1
##  $ important: chr "very important info!"

Data objects can also be constructed directly from raw data with the xts constructor function, in essentially the same way a zoo object is created with the exception that at present there is no equivelant zooreg class.

xts(1:10, Sys.Date()+1:10)
##            [,1]
## 2015-05-06    1
## 2015-05-07    2
## 2015-05-08    3
## 2015-05-09    4
## 2015-05-10    5
## 2015-05-11    6
## 2015-05-12    7
## 2015-05-13    8
## 2015-05-14    9
## 2015-05-15   10

Subsetting

One of the primary motivations, and key points of differentiation of the time series class xts, is the ability to subset rows by specifying ISO-8601 compatible range strings. This allows for natural range-based time queries without requiring prior knowledge of the underlying time object used in construction.

When a raw character vector is used for the i subset argument, it is processed as if it was ISO-8601 compliant. This means that it is parsed from left to right, according to the following specification:

CCYYMMDD HH:MM:SS.ss+

A full description will be expanded from a left-specified truncated one.

Additionally, one may specify range-based queries by simply supplying two time descriptions seperated by a forward slash:

CCYYMMDD HH:MM:SS.ss+/CCYYMMDD HH:MM:SS.ss

Extract a specific date

matrix_xts['2007-01-06']
##             Open  High   Low Close
## 2007-01-06 50.24 50.24 50.11 50.18

Extract the entire month of March

matrix_xts['2007-03']
##             Open  High   Low Close
## 2007-03-01 50.82 50.82 50.56 50.57
## 2007-03-02 50.61 50.72 50.51 50.62
## 2007-03-03 50.73 50.73 50.41 50.41
## 2007-03-04 50.39 50.41 50.25 50.33
## 2007-03-05 50.27 50.34 50.27 50.30
## 2007-03-06 50.27 50.32 50.16 50.16
## 2007-03-07 50.14 50.20 49.91 49.91
## 2007-03-08 49.93 50.00 49.85 49.92
## 2007-03-09 49.92 49.92 49.74 49.81
## 2007-03-10 49.79 49.89 49.70 49.89
## 2007-03-11 49.83 49.88 49.76 49.79
## 2007-03-12 49.83 49.90 49.67 49.74
## 2007-03-13 49.70 49.71 49.38 49.38
## 2007-03-14 49.36 49.54 49.31 49.54
## 2007-03-15 49.57 49.62 49.40 49.50
## 2007-03-16 49.45 49.65 49.42 49.60
## 2007-03-17 49.56 49.56 49.34 49.35
## 2007-03-18 49.30 49.68 49.30 49.65
## 2007-03-19 49.63 49.65 49.52 49.55
## 2007-03-20 49.60 49.62 49.42 49.51
## 2007-03-21 49.50 49.54 49.42 49.52
## 2007-03-22 49.42 49.42 49.31 49.40
## 2007-03-23 49.27 49.27 48.93 48.93
## 2007-03-24 48.87 48.87 48.53 48.53
## 2007-03-25 48.51 48.51 48.33 48.34
## 2007-03-26 48.34 48.45 48.29 48.29
## 2007-03-27 48.25 48.42 48.24 48.31
## 2007-03-28 48.33 48.54 48.33 48.54
## 2007-03-29 48.59 48.70 48.57 48.70
## 2007-03-30 48.75 49.00 48.75 48.94
## 2007-03-31 48.96 49.10 48.96 48.97

Extract all the data from the beginning through January 7, 2007.

matrix_xts['/2007-01-07']
##             Open  High   Low Close
## 2007-01-02 50.04 50.12 49.95 50.12
## 2007-01-03 50.23 50.42 50.23 50.40
## 2007-01-04 50.42 50.42 50.26 50.33
## 2007-01-05 50.37 50.37 50.22 50.33
## 2007-01-06 50.24 50.24 50.11 50.18
## 2007-01-07 50.13 50.22 49.99 49.99

Extract all the data from June 25, 2007 through the end.

matrix_xts['2007-06-25/']
##             Open  High   Low Close
## 2007-06-25 47.20 47.43 47.13 47.43
## 2007-06-26 47.44 47.62 47.44 47.62
## 2007-06-27 47.62 47.72 47.60 47.63
## 2007-06-28 47.68 47.70 47.57 47.61
## 2007-06-29 47.64 47.78 47.62 47.66
## 2007-06-30 47.67 47.94 47.67 47.77

Extract data from May 28, 2007 through June 2, 2007

matrix_xts['2007-05-28/2007-06-02']
##             Open  High   Low Close
## 2007-05-28 47.90 47.93 47.65 47.65
## 2007-05-29 47.66 47.89 47.65 47.87
## 2007-05-30 47.79 47.93 47.79 47.83
## 2007-05-31 47.83 47.84 47.74 47.74
## 2007-06-01 47.74 47.74 47.55 47.65
## 2007-06-02 47.60 47.75 47.57 47.73

see help("[.xts") for more details and examples for extracting subsets of xts objects.

About

Authors: Jeffrey A. Ryan and Joshua M. Ulrich

xts on CRAN

xts on github