Dirk Eddelbuettel — written Jan 19, 2013 — source
The xts package by Jeff Ryan and Josh Ulrich is an immensely powerful tool that is widely used for timeseries work with R. Recently, the question about how to use it from Rcpp came up on StackOverflow and in a thread on the rcpp-devel list.
In fact, xts has had an exposed API since 2008, but it wasn’t used
and as I found out also not quite for two key functions. Jeff kindly gave me SVN access,
and I updated init.c
(to export) and a new xtsAPI.h
header (access these).
This short post will show how to access this functionality using the new RcppXts package.
We start by repeating the (updated) createXts()
function from the
previous post on xts and Rcpp. This
helper function (or an improved version of it) should probably go into RcppXts.
[,1] 1970-01-03 2 1970-01-04 3 1970-01-05 4 1970-01-06 5
Next, we show how to use this. Rcpp attributes will find the xts
header file if use a depends()
attribute, and as the functions we
access are registered with the surrounding R process, no linking is
needed.
Thanks to this new (tree-line !!) function, we can combine xts
object at the source level.
[,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
[,1] 1970-01-03 2 1970-01-04 3 1970-01-05 4 1970-01-05 4 1970-01-06 5 1970-01-06 5 1970-01-07 6 1970-01-08 7 1970-01-09 8 1970-01-10 9
Notice the difference between the results driven by the third
argument about removal of duplicates which has a default value of TRUE
.
While this example was obviously very simple, we can see the power and promise of this.
It derives from being able to work on large numbers of xts
objects directly at the C++
level without having to call back to R. So even though
xts is about as efficient as it gets, we
should be able to make nice gains (for simple enough tasks) by doing them at the C++ level.
tags: xts
Tweet