[R] x[0]: Can '0' be made an allowed index in R?

Hans W hwborcher@ @end|ng |rom gm@||@com
Mon Apr 22 15:45:55 CEST 2024


Thanks for pointing out the 'Oarray' package which indeed for me works
slightly better than the 'index0' package.

    library(Oarray)
    x <- Oarray(data=c(2,3,5,7,11,13,17,19), offset=0)
    x
    #> [0,] [1,] [2,] [3,] [4,] [5,] [6,] [7,]
    #>    2    3    5    7   11   13   17   19

Though I would have preferred it throws an error if the index is
outside the allowed range 0..7 . On the other hand, there is still
some peculiar behavior such as

    x[8]            #> [1] NA
    x[8] <- 23
    x[8]            #> [1] NA

I would like to thank all other posters, incl. the 'small believers'
who think I am doing something wrong. I have written code in more than
20 programming languages, half of them start with index '0', so I am
not taking sides. I really admire those who do this lightheartedly.





On Sun, 21 Apr 2024 at 13:36, Ben Bolker <bbolker using gmail.com> wrote:
>
> https://cran.r-project.org/package=index0
>
> On Sun, Apr 21, 2024, 3:56 AM Hans W <hwborchers using gmail.com> wrote:
>>
>> As we all know, in R indices for vectors start with 1, i.e, x[0] is not a
>> correct expression. Some algorithms, e.g. in graph theory or combinatorics,
>> are much easier to formulate and code if 0 is an allowed index pointing to
>> the first element of the vector.
>>
>> Some programming languages, for instance Julia (where the index for normal
>> vectors also starts with 1), provide libraries/packages that allow the user
>> to define an index range for its vectors, say 0:9 or 10:20 or even negative
>> indices.
>>
>> Of course, this notation would only be feasible for certain specially
>> defined vectors. Is there a library that provides this functionality?
>> Or is there a simple trick to do this in R? The expression 'x[0]' must
>> be possible, does this mean the syntax of R has to be twisted somehow?
>>
>> Thanks, Hans W.
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list