Creating xts objects from source

January 17, 2013

(This article was first published on Rcpp Gallery, and kindly contributed to R-bloggers)

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:

foo <- createXts(1, 10) 
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
[1] 10  1

 [1]  86400 172800 259200 345600 432000 518400 604800 691200 777600 864000
[1] "UTC"
[1] "Date"

[1] "xts" "zoo"

[1] "Date"

[1] "Date"

[1] "UTC"

[1] "UTC"

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

bar <- xts(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.

To leave a comment for the author, please follow the link and comment on their blog: Rcpp Gallery. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

plotly webpage

dominolab webpage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)