[R] Zero Index Origin?

Gabor Grothendieck ggrothendieck at myway.com
Wed Mar 31 15:19:38 CEST 2004

If you are willing to do it yourself you can define a class 
for which indexing behaves that way.

For example, here is a start for a limited implementation for
vectors.  The first statement defines the constructor, the
second defines [, the third converts an index 0 based vector
back to a regular vector, the next implements lvalues and
the last provides print method.

as.vector0 <- function(x) structure(x, class="vector0")
"[.vector0" <- function(x,i) as.vector0(as.vector.vector0(x)[i+1])
as.vector.vector0 <- function(x) unclass(x)
"[<-.vector0" <- function(x,i,value) { 
              x <- as.vector.vector0(x)
              x[i+1] <- value
print.vector0 <- function(x) print(as.vector.vector0(x))

# Test:
x <- as.vector0(1:10)
x[0:4] <- 100 * x[0:4]

Bob Cain <arcane <at> arcanemethods.com> writes:

: I'm very new to R and utterly blown away by not only the 
: language but the unbelievable set of packages and the 
: documentation and the documentation standards and...
: I was an early APL user and never lost my love for it and in 
: R I find most of the essential things I loved about APL 
: except for one thing.  At this early stage of my learning I 
: can't yet determine if there is a way to effect what in APL 
: was zero index origin, the ordinality of indexes starts with 
: 0 instead of 1.  Is it possible to effect that in R without 
: a lot of difficulty?
: I come here today from the world of DSP research and 
: development where Matlab has a near hegemony.  I see no 
: reason whatsoever that R couldn't replace it with a _far_ 
: better and _far_ less idiosyncratic framework.  I'd be 
: interested in working on a Matlab equivalent DSP package for 
: R (if that isn't being done by someone) and one of the 
: things most criticized about Matlab from the standpoint of 
: the DSP programmer is its insistence on 1 origin indexing. 
: Any feedback greatly appreciated.
: Thanks,
: Bob

More information about the R-help mailing list