Creating xts objects from source

Dirk Eddelbuettel — written Jan 17, 2013 — source

A recent post showed how to access the attributes of an xts object. We used an xts object as these are powerful and popular—but any R object using attributed could be used to illustrate the point.

In this short post, we show how one can also do the inverse in order to create an xts object at the C++ source level.

We use a somewhat useless object with values from 1:10 index by dates in the same range. As zero corresponds to the epoch, these will be early 1970-dates. But the values do not matter when showing the principle.

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
Rcpp::NumericVector createXts(int sv, int ev) {

    IntegerVector ind = seq(sv, ev);     // values

    NumericVector dv(ind);               // date(time)s are real values
    dv = dv * 86400;                     // scaled to days
    dv.attr("tzone")    = "UTC";         // the index has attributes
    dv.attr("tclass")   = "Date";

    NumericVector xv(ind);               // data her same index
    xv.attr("dim")         = IntegerVector::create(ev-sv+1,1);
    xv.attr("index")       = dv;
    CharacterVector klass  = CharacterVector::create("xts", "zoo");
    xv.attr("class")       = klass;
    xv.attr(".indexCLASS") = "Date";
    xv.attr("tclass")      = "Date";
    xv.attr(".indexTZ")    = "UTC";
    xv.attr("tzone")       = "UTC";
    
    return xv;

}

We can run this function, and look at the (numerous) attributes in the generated object:

suppressMessages(library(xts))
foo <- createXts(1, 10) 
foo
           [,1]
1970-01-02    1
1970-01-03    2
1970-01-04    3
1970-01-05    4
1970-01-06    5
1970-01-07    6
1970-01-08    7
1970-01-09    8
1970-01-10    9
1970-01-11   10
attributes(foo)
$dim
[1] 10  1

$index
 [1]  86400 172800 259200 345600 432000 518400 604800 691200 777600 864000
attr(,"tzone")
[1] "UTC"
attr(,"tclass")
[1] "Date"

$class
[1] "xts" "zoo"

$.indexCLASS
[1] "Date"

$tclass
[1] "Date"

$.indexTZ
[1] "UTC"

$tzone
[1] "UTC"

It turns out that creating an xts object the usual way creates an object that is equal:

bar <- xts(1:10, order.by=as.Date(1:10)) 
all.equal(foo, bar)
[1] TRUE

So now we can create xts objects at the source level.

Upated to add sv and ev start and end values.

tags: basics  xts 

Related Articles