[R] Zero Index Origin?
Tony Plate
tplate at blackmesacapital.com
Thu Apr 1 17:56:05 CEST 2004
As far as I can see, the problems with legacy functions kill the
class-based idea. As others have noted, if you define a class for which
the indexing function "[" is zero-based, and then pass an object x of that
class to a legacy function, anything that function does that assumes 1 is
the first index and length(x) is the last index and uses "[" will
fail. Other problems also arise, e.g., if x is a zero-based array, and y
is a standard array, then what are the index bases of x+y and y+x? You
could make this depend on the order of arguments, but I suspect this would
lead to horrible programming situations.
Anyway, it seems that what you want to do is change the behavior of the
indexing operation, not the behavior of the objects themselves. So, as
Richard A. O'Keefe suggested, a more feasible approach would be to define a
new indexing function that was zero-based. E.g., something like
zsub(). So, zsub(x, 0, 0) would return the upper left element of the
matrix x. This would not be too hard to write (and make it work for arrays
with any number of dimensions). If you wanted, you could also define an
operator to access this function, e.g., so that x^[0,0] returned the upper
left element of the matrix x, i.e., "^[" does zero-based
indexing. However, this is a lot more work and requires modifying the
source code of R. As noted by others, this zero-based indexing operator
would not be able to use negative indices, but you could still use the
standard indexing operators to get that feature.
I've cursed the one-based indexing in S-derived languages on the rare
occasions when I've wanted to do index arithmetic (i.e., work out the
linear locations of array elements based on their indexes) -- it was a pain
to always have to subtract one from indices before multiplying and then add
one back in at the end. However, this really was a minor inconvenience,
and if I wanted I could have written a function to do my index computations
for me. What other more significant computations are so much easier with
zero-based indexing?
-- Tony Plate
At Wednesday 10:43 PM 3/31/2004, Bob Cain wrote:
>Gabor Grothendieck wrote:
>
>[snip good stuff]
>
>>Of course the above is motherhood and some specific examples
>>might put a sharper edge to the discussion.
>
>I really appreciate your point of view on this and think you
>are probably right. A question I have from my very limited
>understanding yet of OO is whether such objects could be
>passed to legacy functions with any expectation of correct
>results.
>
>
>Thanks,
>
>Bob
>--
>
>"Things should be described as simply as possible, but no
>simpler."
>
> A. Einstein
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list