@@ -25,7 +25,7 @@ using namespace Rcpp;
25
25
26
26
// [[ Rcpp::export]]
27
27
std::vector< std::string > xtsAttributes(NumericMatrix X) {
28
- std::vector< std::string > nm = RObject(X) .attributeNames();
28
+ std::vector< std::string > nm = X .attributeNames();
29
29
return nm;
30
30
}
31
31
{% endhighlight %}
@@ -35,38 +35,38 @@ A first example simply creates a random `xts` object of twenty observations.
35
35
We then examine the set of attributes and return it in a first program.
36
36
37
37
{% highlight r %}
38
- suppressMessages(library(xts))
39
- set.seed(42)
40
- n <- 20
41
- Z <- xts(100+cumsum(rnorm(n)), order.by=ISOdatetime(2013,1,12,20,21,22) + 60* (1: n ))
42
- xtsAttributes(Z)
38
+ suppressMessages(library(xts))
39
+ set.seed(42)
40
+ n <- 20
41
+ Z <- xts(100+cumsum(rnorm(n)), order.by=ISOdatetime(2013,1,12,20,21,22) + 60* (1: n ))
42
+ xtsAttributes(Z)
43
43
{% endhighlight %}
44
44
45
45
46
46
47
47
<pre class =" output " >
48
- [1] " dim" " index" " class" " .indexCLASS" " tclass"
49
- [6] " .indexTZ" " tzone"
48
+ [1] & quot ; dim& quot ; & quot ; index& quot ; & quot ; class& quot ; & quot ; .indexCLASS& quot ; & quot ; tclass& quot ;
49
+ [6] & quot ; .indexTZ& quot ; & quot ; tzone& quot ;
50
50
</pre >
51
51
52
52
53
53
The same result is seen directly in R:
54
54
55
55
{% highlight r %}
56
- names(attributes(Z))
56
+ names(attributes(Z))
57
57
{% endhighlight %}
58
58
59
59
60
60
61
61
<pre class =" output " >
62
- [1] " dim" " index" " class" " .indexCLASS" " tclass"
63
- [6] " .indexTZ" " tzone"
62
+ [1] & quot ; dim& quot ; & quot ; index& quot ; & quot ; class& quot ; & quot ; .indexCLASS& quot ; & quot ; tclass& quot ;
63
+ [6] & quot ; .indexTZ& quot ; & quot ; tzone& quot ;
64
64
</pre >
65
65
66
66
67
67
68
68
{% highlight r %}
69
- all.equal(xtsAttributes(Z), names(attributes(Z)))
69
+ all.equal(xtsAttributes(Z), names(attributes(Z)))
70
70
{% endhighlight %}
71
71
72
72
@@ -84,31 +84,34 @@ we have a `Datetime` object so we can instantiate it at the C++ level.
84
84
{% highlight cpp %}
85
85
// [[ Rcpp::export]]
86
86
DatetimeVector xtsIndex(NumericMatrix X) {
87
- DatetimeVector v(NumericVector(RObject(X) .attr("index")));
87
+ DatetimeVector v(NumericVector(X .attr("index")));
88
88
return v;
89
89
}
90
90
{% endhighlight %}
91
91
92
92
93
93
{% highlight r %}
94
- xtsIndex(Z)
94
+ xtsIndex(Z)
95
95
{% endhighlight %}
96
96
97
97
98
98
99
99
<pre class =" output " >
100
- [1] " 2013-01-12 20:22:22 CST" " 2013-01-12 20:23:22 CST"
101
- [3] " 2013-01-12 20:24:22 CST" " 2013-01-12 20:25:22 CST"
102
- [5] " 2013-01-12 20:26:22 CST" " 2013-01-12 20:27:22 CST"
103
- [7] " 2013-01-12 20:28:22 CST" " 2013-01-12 20:29:22 CST"
104
- [9] " 2013-01-12 20:30:22 CST" " 2013-01-12 20:31:22 CST"
105
- [11] " 2013-01-12 20:32:22 CST" " 2013-01-12 20:33:22 CST"
106
- [13] " 2013-01-12 20:34:22 CST" " 2013-01-12 20:35:22 CST"
107
- [15] " 2013-01-12 20:36:22 CST" " 2013-01-12 20:37:22 CST"
108
- [17] " 2013-01-12 20:38:22 CST" " 2013-01-12 20:39:22 CST"
109
- [19] " 2013-01-12 20:40:22 CST" " 2013-01-12 20:41:22 CST"
100
+ [1] & quot ; 2013-01-12 20:22:22 CST& quot ; & quot ; 2013-01-12 20:23:22 CST& quot ;
101
+ [3] & quot ; 2013-01-12 20:24:22 CST& quot ; & quot ; 2013-01-12 20:25:22 CST& quot ;
102
+ [5] & quot ; 2013-01-12 20:26:22 CST& quot ; & quot ; 2013-01-12 20:27:22 CST& quot ;
103
+ [7] & quot ; 2013-01-12 20:28:22 CST& quot ; & quot ; 2013-01-12 20:29:22 CST& quot ;
104
+ [9] & quot ; 2013-01-12 20:30:22 CST& quot ; & quot ; 2013-01-12 20:31:22 CST& quot ;
105
+ [11] & quot ; 2013-01-12 20:32:22 CST& quot ; & quot ; 2013-01-12 20:33:22 CST& quot ;
106
+ [13] & quot ; 2013-01-12 20:34:22 CST& quot ; & quot ; 2013-01-12 20:35:22 CST& quot ;
107
+ [15] & quot ; 2013-01-12 20:36:22 CST& quot ; & quot ; 2013-01-12 20:37:22 CST& quot ;
108
+ [17] & quot ; 2013-01-12 20:38:22 CST& quot ; & quot ; 2013-01-12 20:39:22 CST& quot ;
109
+ [19] & quot ; 2013-01-12 20:40:22 CST& quot ; & quot ; 2013-01-12 20:41:22 CST& quot ;
110
110
</pre >
111
111
112
112
113
113
Further operations such as subsetting based on the datetime vector
114
114
or adjustments to time zones are left as an exercise.
115
+
116
+ Edited on 2014-03-28 to reflect updated / simpliefied attributes functions.
117
+
0 commit comments