[R] Zero Index Origin?

kjetil@entelnet.bo kjetil at entelnet.bo
Thu Apr 1 17:32:36 CEST 2004


On 31 Mar 2004 at 18:31, Bob Cain wrote:

> 
> 
> Peter Dalgaard wrote:
> 
> 
> > I think it was said quite early in the thread, but since noone
> > apparently listened, let me reiterate: One of the powerful indexing
> > features in R is the negative index ("all, except") and x[-0] would
> > lead to some complications if 0 was a true index. 

Yes: Look at this: (R1.8.1)

> library(Oarray)

Attaching package 'Oarray':


        The following object(s) are masked from package:base :

         as.array 

> test <- Oarray(1:10, offset=0)
> test
[0,] [1,] [2,] [3,] [4,] [5,] [6,] [7,] [8,] [9,] 
   1    2    3    4    5    6    7    8    9   10 
> str(test)
 int [, 1:10] 2 3 4 5 6 7 8 9 10 NA
 - attr(*, "offset")= num 0
 - attr(*, "drop.negative")= logi TRUE
 - attr(*, "class")= chr "Oarray"
> test[0]
[1] 1
> xx <- 1:10
> test==xx
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> xx[0]
numeric(0)
> 0==FALSE
[1] TRUE
> test[FALSE]
numeric(0)
> test[0]
[1] 1
> test[-0]
[1] 1


Kjetil Halvorsen


> > 
> 
> Interesting.  The kind of gotcha I hoped wouldn't exist. 
> Others?
> 
> I've thought of another one.  Legacy functions that accept 
> indices as arguments from a 0-origin caller.
> 
> 
> 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