# [Rcpp-devel] Ranges and Casting

Francisco Bischoff fbischoff at med.up.pt
Sat Jan 16 03:05:05 CET 2021

```Thanks for the tips.

Still, Range() is the correct way to write:

NumericVector my_vec(200);

my_vec(Range(0,10)) = 10;

?

--
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
@ cintesis.eu
- Teaching Assistant | department of community medicine, information and
health decision sciences @ med.up.pt

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

On Sat, Jan 16, 2021 at 1:54 AM Dirk Eddelbuettel <edd at debian.org> wrote:

>
> On 16 January 2021 at 01:35, Francisco Bischoff wrote:
> | About the Range() function, I think it should handle decreasing ranges
> | too...
> | But, idk if using Matlabs approach or R approach:
> |
> | R's:
> |
> | a <- 1
> | b <- 10
> |
> | print(a:b)
> | 1 2 3 4 5 6 7 8 9 10
> | print(b:a)
> | 10 9 8 7 6 5 4 3 2 1
> |
> | Matlab's
> | a = 1;
> | b = 10;
> | disp(a:b);
> | 1 2 3 4 5 6 7 8 9 10
> | disp(b:a);
> | numeric(0)
> | disp(b:-1:a)
> | 10 9 8 7 6 5 4 3 2 1
> |
> | I think that we are in the R domain, so we should use Range(b, a)
> normally
> | (my humble opinion).
>
> Hm but I think the Range class in Rcpp is not the same as R's seq(). It is
> used internally in a few places and may not generalize well to the 'b:-1:a'
> use.
>
> I had a quick look, and Armadillo may not have anything directly relevant
> either. linspace() is close but not quite the same.
>
> I would probably just write myself a little helper function.
>
> | About Casting, this is a good example:
> |
> | Rcout << Range(0, 10) << std::endl;
> |
> | This does not compute...
> |
> | What should I do?
> |
> | Rcout << as<IntegerVector>(Range(0, 10)) << std::endl;
> | or
> | Rcout << (IntegerVector)(Range(0, 10)) << std::endl;
>
> That can happen as template expressions can get in the way. Alternatives
> are
> maybe using Armadillo types for, again, just using a quick loop and
> printing
> elements.
>
> Rcpp has a lot of functions. But it doesn't have "all of R's functions" and
> should not be seen as aiming for that.
>
> Dirk
>
> --
> https://dirk.eddelbuettel.com