Skip to content

index range

4 messages · hill0093, Gabor Grothendieck, Ben Bolker

#
It looks to me like the index range starts at 1 in R.
Is this true?
If so, is there a way to change it to start at 0?
That way, I wouldn't have to make so many
changes when I translate a function from
another language.
#
You can define origin 0 objects yourself if you like.
Here is a partial implementation:

"[.orig0" <- function(x, i)
    if (is.numeric(i)) .subset(x, i+0) else .subset(x, i)
orig0 <- function(x)
    structure(x, class = c("orig0", setdiff(class(x), "orig0")))
x <- orig0(1:5)
x[0:3] # 1:4

Note that usually 0 means leave out that element in R and
in this implementation -1 means leave it out.  Also normally
-3 mean leave out 3rd element but in the implementation
above -0 would be 0 so it would give the first element.

Probably best to just get used to the R way.
On Feb 18, 2008 6:31 PM, hill0093 <hill0093 at umn.edu> wrote:
#
On Feb 18, 2008 6:52 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
The 0 should be a 1.