Skip to content

Commit 2491ed3

Browse files
committed
update to show current / simplified attributes use
1 parent c2ec784 commit 2491ed3

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

_posts/2013-01-12-getting-attributes-for-xts-example.md

+27-24
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace Rcpp;
2525

2626
// [[Rcpp::export]]
2727
std::vector<std::string> xtsAttributes(NumericMatrix X) {
28-
std::vector<std::string> nm = RObject(X).attributeNames();
28+
std::vector<std::string> nm = X.attributeNames();
2929
return nm;
3030
}
3131
{% endhighlight %}
@@ -35,38 +35,38 @@ A first example simply creates a random `xts` object of twenty observations.
3535
We then examine the set of attributes and return it in a first program.
3636

3737
{% 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)
4343
{% endhighlight %}
4444

4545

4646

4747
<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;
5050
</pre>
5151

5252

5353
The same result is seen directly in R:
5454

5555
{% highlight r %}
56-
names(attributes(Z))
56+
names(attributes(Z))
5757
{% endhighlight %}
5858

5959

6060

6161
<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;
6464
</pre>
6565

6666

6767

6868
{% highlight r %}
69-
all.equal(xtsAttributes(Z), names(attributes(Z)))
69+
all.equal(xtsAttributes(Z), names(attributes(Z)))
7070
{% endhighlight %}
7171

7272

@@ -84,31 +84,34 @@ we have a `Datetime` object so we can instantiate it at the C++ level.
8484
{% highlight cpp %}
8585
// [[Rcpp::export]]
8686
DatetimeVector xtsIndex(NumericMatrix X) {
87-
DatetimeVector v(NumericVector(RObject(X).attr("index")));
87+
DatetimeVector v(NumericVector(X.attr("index")));
8888
return v;
8989
}
9090
{% endhighlight %}
9191

9292

9393
{% highlight r %}
94-
xtsIndex(Z)
94+
xtsIndex(Z)
9595
{% endhighlight %}
9696

9797

9898

9999
<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;
110110
</pre>
111111

112112

113113
Further operations such as subsetting based on the datetime vector
114114
or adjustments to time zones are left as an exercise.
115+
116+
Edited on 2014-03-28 to reflect updated / simpliefied attributes functions.
117+

src/2013-01-12-getting-attributes-for-xts-example.cpp

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using namespace Rcpp;
2323

2424
// [[Rcpp::export]]
2525
std::vector<std::string> xtsAttributes(NumericMatrix X) {
26-
std::vector<std::string> nm = RObject(X).attributeNames();
26+
std::vector<std::string> nm = X.attributeNames();
2727
return nm;
2828
}
2929

@@ -33,20 +33,20 @@ std::vector<std::string> xtsAttributes(NumericMatrix X) {
3333
*/
3434

3535
/*** R
36-
suppressMessages(library(xts))
37-
set.seed(42)
38-
n <- 20
39-
Z <- xts(100+cumsum(rnorm(n)), order.by=ISOdatetime(2013,1,12,20,21,22) + 60*(1:n))
40-
xtsAttributes(Z)
36+
suppressMessages(library(xts))
37+
set.seed(42)
38+
n <- 20
39+
Z <- xts(100+cumsum(rnorm(n)), order.by=ISOdatetime(2013,1,12,20,21,22) + 60*(1:n))
40+
xtsAttributes(Z)
4141
*/
4242

4343
/**
4444
* The same result is seen directly in R:
4545
*/
4646

4747
/*** R
48-
names(attributes(Z))
49-
all.equal(xtsAttributes(Z), names(attributes(Z)))
48+
names(attributes(Z))
49+
all.equal(xtsAttributes(Z), names(attributes(Z)))
5050
*/
5151

5252

@@ -60,16 +60,20 @@ std::vector<std::string> xtsAttributes(NumericMatrix X) {
6060

6161
// [[Rcpp::export]]
6262
DatetimeVector xtsIndex(NumericMatrix X) {
63-
DatetimeVector v(NumericVector(RObject(X).attr("index")));
63+
DatetimeVector v(NumericVector(X.attr("index")));
6464
return v;
6565
}
6666

6767
/*** R
68-
xtsIndex(Z)
68+
xtsIndex(Z)
6969
*/
7070

7171
/**
7272
* Further operations such as subsetting based on the datetime vector
7373
* or adjustments to time zones are left as an exercise.
7474
*/
7575

76+
/**
77+
* Edited on 2014-03-28 to reflect updated / simpliefied attributes functions.
78+
*
79+
*/

0 commit comments

Comments
 (0)