Creating xts objects from source

[This article was first published on Rcpp Gallery, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: Rcpp Gallery.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)