Dirk Eddelbuettel — written Jan 12, 2013 — source
An earlier post illustrated that R object attributes can be set at the C++ level. Naturally, we can also read them from an object. This proves particularly useful for xts objects which are, in essence, numerical matrices with added attributed that are used by a rich set of R operators and functions.
Here, we show how to access these attributes.
A first example simply creates a random xts
object of twenty observations.
We then examine the set of attributes and return it in a first program.
[1] "dim" "index" "class" ".indexCLASS" "tclass" [6] ".indexTZ" "tzone"
The same result is seen directly in R:
[1] "dim" "index" "class" ".indexCLASS" "tclass" [6] ".indexTZ" "tzone"
[1] TRUE
Now, given the attributes we can of course access some of these.
The index()
function xts
objects returns the index. Here, we know
we have a Datetime
object so we can instantiate it at the C++ level.
(Real production code would test types etc).
[1] "2013-01-12 20:22:22 CST" "2013-01-12 20:23:22 CST" [3] "2013-01-12 20:24:22 CST" "2013-01-12 20:25:22 CST" [5] "2013-01-12 20:26:22 CST" "2013-01-12 20:27:22 CST" [7] "2013-01-12 20:28:22 CST" "2013-01-12 20:29:22 CST" [9] "2013-01-12 20:30:22 CST" "2013-01-12 20:31:22 CST" [11] "2013-01-12 20:32:22 CST" "2013-01-12 20:33:22 CST" [13] "2013-01-12 20:34:22 CST" "2013-01-12 20:35:22 CST" [15] "2013-01-12 20:36:22 CST" "2013-01-12 20:37:22 CST" [17] "2013-01-12 20:38:22 CST" "2013-01-12 20:39:22 CST" [19] "2013-01-12 20:40:22 CST" "2013-01-12 20:41:22 CST"
Further operations such as subsetting based on the datetime vector or adjustments to time zones are left as an exercise.
Edited on 2014-03-28 to reflect updated / simpliefied attributes functions.
Tweet