[R] Zero Index Origin?

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Apr 1 04:43:04 CEST 2004

I note that (1) "[" is a class-based function in R, so it would be
possible to define a class of zero-origin arrays.  This would mean
that indexing these things would be quite incompatible with all the
other indexing in R, so it's not clear that it would be a good thing.
(2) R lets you define "left-hand functions", so if you define
    sub <- function (a, i) a[i+1]
    "sub<-" <- function (a, i, value) a[i+1] <- value
then you can use sub(a,i) and sub(a,i) <- ... for indexing.  This
still wouldn't work like R indexing in general, but it wouldn't
_look_ as if it should, so that might be better.

I used to love APL myself, and am a great fan of index origin 0, but
I'm *not* a fan of having two different origins in one language
controlled by a variable; it made life quite difficult trying to mix
code from different libraries.  

In R, I find myself wondering whether a["fred"] (string indexing)
should depend on index origin, and if not, why not.  (:-)

Despite my love for origin 0, I've decided that the rest of R is worth it.

Despite my love for origin 0, I've found that for the things that I do
with R, index origin 1 really does seem to work better as a human

It would be interesting to see some sample code where origin 0 is supposed
to make life easier, and to see what R experts to do make it even easier
than that.

More information about the R-help mailing list