<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hello,<div><br></div><div>About the Range() function, I think it should handle decreasing ranges too...</div><div>But, idk if using Matlabs approach or R approach:</div><div><br></div><div>R's:</div><div><br></div><div><font face="monospace">a <- 1</font></div><div><font face="monospace">b <- 10</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">print(a:b)</font></div><div><font face="monospace">1 2 3 4 5 6 7 8 9 10</font></div><div><font face="monospace">print(b:a)<br></font></div><div><font face="monospace">10 9 8 7 6 5 4 3 2 1</font></div><div><br></div><div>Matlab's</div><div><font face="monospace">a = 1;</font></div><div><font face="monospace">b = 10;</font></div><div><font face="monospace">disp(a:b);</font></div><div><font face="monospace">1 2 3 4 5 6 7 8 9 10<br></font></div><div><font face="monospace">disp(b:a);<br></font></div><div><font face="monospace">numeric(0)</font></div><div><font face="monospace">disp(b:-1:a)</font></div><div><font face="monospace">10 9 8 7 6 5 4 3 2 1</font><br></div><div><br clear="all"><div><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><span style="font-size:12.8px">I think that we are in the R domain, so we should use Range(b, a) normally (my humble opinion).</span></div><div dir="ltr"><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">About Casting, this is a good example:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px"><font face="monospace">Rcout << Range(0, 10) << std::endl;</font></span></div><div><span style="font-size:12.8px"><font face="monospace"><br></font></span></div><div><span style="font-size:12.8px"><font face="arial, sans-serif">This does not compute...</font></span></div><div><span style="font-size:12.8px"><font face="arial, sans-serif"><br></font></span></div><div><span style="font-size:12.8px"><font face="arial, sans-serif">What should I do?</font></span></div><div><span style="font-size:12.8px"><font face="arial, sans-serif"><br></font></span></div><div><font face="monospace">Rcout << as<IntegerVector>(Range(0, 10)) << std::endl;<span style="font-size:12.8px"><br></span></font></div><div><font face="arial, sans-serif">or</font></div><div><font face="monospace">Rcout << (IntegerVector)(Range(0, 10)) << std::endl;</font><br></div><div dir="ltr"><span style="font-size:12.8px"><br></span></div><div dir="ltr"><span style="font-size:12.8px">--</span><br style="font-size:12.8px"><span style="font-size:large">Francisco Bischoff, MD, MSc</span></div><div>Faculty of Medicine of the University of Porto, Portugal<br></div><div><br></div><div dir="ltr">- Master of Medical Informatics | topic: time series<br>- Research Associate | <a href="https://mailtrack.io/trace/link/261f22c687548623a329c468d9ee8e2177eb0450?url=http%3A%2F%2Fcintesis.eu%2Fai4health%2F&userId=3785237&signature=3e0be0b7841b95c4" target="_blank">artificial intelligence for health</a> @ <a href="https://mailtrack.io/trace/link/37900be19872e834994af798ab1d53480613e66c?url=http%3A%2F%2Fcintesis.eu&userId=3785237&signature=91ac56f3d356e8bd" target="_blank">cintesis.eu</a><br>- Teaching Assistant | department of community medicine, information and health decision sciences @ <a href="https://mailtrack.io/trace/link/6951087469cd0e54d4bc365eedae9d9ad24d5970?url=http%3A%2F%2Fmed.up.pt&userId=3785237&signature=ac6fffd4ce8944ca" target="_blank">med.up.pt</a><br><br>ORCID: 0000-0002-5301-8672 | Mendeley: francisco-bischoff | Google: tCeA0uUAAAAJ | ResearcherID: H-8261-2016 | ResearchGate: Francisco_Bischoff | CiênciaID B413-E0A0-DE8D | LinkedIn: franzbischoff</div></div></div></div></div></div></div></div></div></div></div></div></div></div><br></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 6, 2021 at 6:11 AM Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Francisco,<br>
<br>
On 5 January 2021 at 13:58, Francisco Bischoff wrote:<br>
| First of all, let me thank all involved with the Rcpp package!<br>
| <br>
| I just have a (maybe silly) question, but I struggle with this frequently<br>
| in my codes:<br>
| <br>
| Range() vs IntegerVector<br>
| <br>
| One example is when I need a reverse index, like R's 10:1, so I had to<br>
| write my own function that returns an IntegerVector. But, to work I have to<br>
| do this ugly code:<br>
| <br>
|           IntegerVector dot_rev_idxs1 = ::seq((i - 1), beginidx);<br>
|           IntegerVector dot_rev_idxs2 = ::seq((idx_nn - 1), (beginidx +<br>
| idx_diff));<br>
|           IntegerVector dot_rev_idxs3 = ::seq((i - 1 + window_size), (<br>
| beginidx + window_size));<br>
|           IntegerVector dot_rev_idxs4 = ::seq((idx_nn - 1 + window_size), (<br>
| beginidx + idx_diff + window_size));<br>
|           dotproduct[dot_rev_idxs1] = (NumericVector)((NumericVector)cumsum(<br>
| data[dot_rev_idxs1] * data[dot_rev_idxs2] -<br>
| <br>
| data[dot_rev_idxs3] * data[dot_rev_idxs4]) +<br>
|                                                       dotproduct[i]);<br>
| <br>
| I frequently have to cast the results with (NumericVector)...<br>
| <br>
| This also happens using Range(). But there is a difference if I use Range()<br>
| this way: my_vector[Range(0,10)]; or Range range = Range(0,<br>
| 10); my_vector[range];<br>
| <br>
| How is the proper way to deal with it?<br>
<br>
I am not sure I fully understand what it is that you try to and what you are<br>
struggling with.<br>
<br>
Could you construct a small self-contained example?<br>
<br>
| Other question: as<NumericVector() doesn't 'cast' right? Does it try to<br>
| 'convert' formats?<br>
| <br>
| And finally, I see that Matlab has a unique way to deal with ranges:<br>
| <br>
| Ex.:<br>
| <br>
| 1) (1:10) --> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10<br>
| 2) (10:1) --> returns a vector with size zero<br>
| 3) (10:-1:1) --> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1<br>
| <br>
| Range() shouldn't at least reproduce the R's counterpart and accept Range(10,1)<br>
| == 10:1?<br>
<br>
We could probably generalize seq() to count down from 'high' to 'low'. Pull<br>
requests welcome...<br>
<br>
Dirk<br>
<br>
-- <br>
<a href="https://mailtrack.io/trace/link/3dc4aa6120269dd41ea957abba6a53feaf80a120?url=https%3A%2F%2Fdirk.eddelbuettel.com&userId=3785237&signature=a0cad3f7fa0cc627" rel="noreferrer" target="_blank">https://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><br>
</blockquote></div><img width="0" height="0" class="mailtrack-img" alt="" style="display:flex" src="https://mailtrack.io/trace/mail/5bea9dfaead251f10f844c393c601ae1d59f2496.png?u=3785237"></div>