[Rcpp-devel] Ranges and Casting

Francisco Bischoff fbischoff at med.up.pt
Tue Jan 5 14:58:25 CET 2021


Hello everyone,

First of all, let me thank all involved with the Rcpp package!

I just have a (maybe silly) question, but I struggle with this frequently
in my codes:

Range() vs IntegerVector

One example is when I need a reverse index, like R's 10:1, so I had to
write my own function that returns an IntegerVector. But, to work I have to
do this ugly code:

          IntegerVector dot_rev_idxs1 = ::seq((i - 1), beginidx);
          IntegerVector dot_rev_idxs2 = ::seq((idx_nn - 1), (beginidx +
idx_diff));
          IntegerVector dot_rev_idxs3 = ::seq((i - 1 + window_size), (
beginidx + window_size));
          IntegerVector dot_rev_idxs4 = ::seq((idx_nn - 1 + window_size), (
beginidx + idx_diff + window_size));
          dotproduct[dot_rev_idxs1] = (NumericVector)((NumericVector)cumsum(
data[dot_rev_idxs1] * data[dot_rev_idxs2] -

data[dot_rev_idxs3] * data[dot_rev_idxs4]) +
                                                      dotproduct[i]);

I frequently have to cast the results with (NumericVector)...

This also happens using Range(). But there is a difference if I use Range()
this way: my_vector[Range(0,10)]; or Range range = Range(0,
10); my_vector[range];

How is the proper way to deal with it?

Other question: as<NumericVector() doesn't 'cast' right? Does it try to
'convert' formats?

And finally, I see that Matlab has a unique way to deal with ranges:

Ex.:

1) (1:10) --> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
2) (10:1) --> returns a vector with size zero
3) (10:-1:1) --> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

Range() shouldn't at least reproduce the R's counterpart and accept Range(10,1)
== 10:1?

Thanks in advance!

my package: https://CRAN.R-project.org/package=tsmp
<https://mailtrack.io/trace/link/9b0962ae6d75a7caaaf14e3c8d77db64148aabc1?url=https%3A%2F%2Fcran.r-project.org%2Fpackage%3Dtsmp&userId=3785237&signature=c65e6b4074bf11ac>

--
Francisco Bischoff, MD, MSc
Faculty of Medicine of the University of Porto, Portugal

- Master of Medical Informatics | topic: time series
- Research Associate | artificial intelligence for health
<https://mailtrack.io/trace/link/93d55bc6ee2a8fc10541d0274f3fde245e944698?url=http%3A%2F%2Fcintesis.eu%2Fai4health%2F&userId=3785237&signature=decc8b53c66739e9>
@ cintesis.eu
<https://mailtrack.io/trace/link/02df11fcc383816c96681205a4142a6ccb60bfaa?url=http%3A%2F%2Fcintesis.eu&userId=3785237&signature=9d174b79c4ee78b7>
- Teaching Assistant | department of community medicine, information and
health decision sciences @ med.up.pt
<https://mailtrack.io/trace/link/50de7c0f37c419aafee22f9492fdfdbd1da63c1d?url=http%3A%2F%2Fmed.up.pt&userId=3785237&signature=66de0e074d671cf7>

ORCID: 0000-0002-5301-8672 | Mendeley: francisco-bischoff | Google:
tCeA0uUAAAAJ | ResearcherID: H-8261-2016 | ResearchGate: Francisco_Bischoff
| CiênciaID B413-E0A0-DE8D | LinkedIn: franzbischoff



*"Quality is not an act, it is a habit."-- Aristotle*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20210105/a14b5774/attachment.html>


More information about the Rcpp-devel mailing list