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

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Tue Apr 23 17:24:54 CEST 2024


I think it might be fair to say that the discussion is becoming a tad wider
than whether you want your data structures indexed starting from 0 or 1.

Programming languages have added functionality to do many things on top of
the simple concept of accessing or changing the nth element one at a time.
If someone wants to make a parallel way to handle things, it may work for
some uses but not others and that is fine as long as the user of the package
knows the limits and is careful.

In R, as has been pointed out, there are behaviors associated with indexing
that are NOT the only way they could have been done. These are design
choices made long ago.

Negated numbers are allowed in some contexts and have meaning. Ranges of
numbers can be specified. Rows and columns both must be accommodated. And
since quite a bit of the language gets written in languages like C/C++ that
may not anticipate such a change, it MIGHT BE that there will be errors if
the way being zero-based is introduced does not work well.

I have not looked at the packages mentioned and it may well be that they are
very carefully crafted in a way that takes everything into account and even
behaves well if you have operations that work jointly on both 0 and 1 based
objects. They may even have a way to identify which way a particular object
is numbered.

As pointed, out programming languages differ. I believe JavaScript at one
point had a data structure that was sort of like a dictionary that treated
integer indexes as a sort of indexed array and did interesting things if
some numbers were missing or added and sort of reindexed it when used as an
array. Features can be very powerful and at the same time be potentially
dangerous. 

But the original request was not only reasonable but also something others
had worked on and produced packages. Chances are good that many of the
questions people had were considered and either implemented well or
documented as something to avoid.

I would like to add that R is evolving as is the way people use it. An
example might be how you can make and use data.frames (including new
variants) with variable list components and do transformations. I have seen
problems when using such features because some operations on them may not do
what is expected. In some cases, I suspect that ideas that are accepted and
moved back into the base after careful consideration are safer. You could
have a global setting for whether ALL operations are  0-based or 1-based or
have several data types to choose from that work as you want. But some of it
is simply engrained in peoples minds as in that programmers sometimes call a
function and index the results as in minmax(x)[2] and that digit 2 is
hardwired in with the assumption they are getting the max value stored in
the second position and ignoring the min value stored in the first position.
Flipping a switch at the top of the program leaves such errors in place.
Many packages include all kinds of shortcuts like that and a global switch
idea may leave many dangling packages.

For the many of us with experiences in many programming languages, all kinds
of mental stumbling blocks abound unless we accept each language as a world
of its own in which different design and implementation choices were made.
If you take a language like Python and ask how to support 1-based objects,
you might well come up with a rather different solution including using
hooks such as the dunder variables  they made easily accessible, making a
subclass, using a decorator and so on.

Having said that, consider that  there are many successful examples in R
where packages have augmented doing things in ways different than base R and
different does not have to be better or worse but can have advantages for
some people in some situations.

An example often discussed here is the a group of packages called the
tidyverse. Some aspects of it are more appealing to me than the parts of
base R I might use to do the same kinds of things and some don't. Within
reason, many parts can be mixed and matched. There do seem to be places the
match is not so great and places where they added too much functionality
that many users do not need and which complicated earlier ways of doing
simpler things. And, of course, it can divide a community when others cannot
understand your code without lots of additional education and new ways of
thinking.

A well-designed way to designate individual data structures to be 0-based
would indeed be nice to have, and apparently it is available. I have seen
other packages mentioned here that in which I worked a bit with the one
making the package and saw how many hooks had to be dealt with to handle
having multiple ways of being "missing" as is available in some programs
outside R. Many implementations only work with known cases and can break
when combined, perhaps with other newer changes to R or packages. But, if
used within a carefully designed environment they may do what you need and
preserve functionality.




-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of John Fox
Sent: Tuesday, April 23, 2024 10:24 AM
To: Peter Dalgaard <pd.mes using cbs.dk>
Cc: R help project <r-help using r-project.org>; Hans W <hwborchers using gmail.com>
Subject: Re: [R] x[0]: Can '0' be made an allowed index in R?

Hello Peter,

Unless I too misunderstand your point, negative indices for removal do 
work with the Oarray package (though -0 doesn't work to remove the 0th 
element, since -0 == 0 -- perhaps what you meant):

 > library(Oarray)

 > v <- Oarray(1:10, offset=0)

 > v
[0,] [1,] [2,] [3,] [4,] [5,] [6,] [7,] [8,] [9,]
    1    2    3    4    5    6    7    8    9   10

 > dim(v)
[1] 10

 > v[-1]
[1]  1  3  4  5  6  7  8  9 10

 > v[-0]
[1] 1

Best,
  John

On 2024-04-23 9:03 a.m., Peter Dalgaard via R-help wrote:
> Caution: External email.
> 
> 
> Doesn't sound like you got the point. x[-1] normally removes the first
element. With 0-based indices, this cannot work.
> 
> - pd
> 
>> On 22 Apr 2024, at 17:31 , Ebert,Timothy Aaron <tebert using ufl.edu> wrote:
>>
>> You could have negative indices. There are two ways to do this.
>> 1) provide a large offset.
>> Offset <- 30
>> for (i in -29 to 120) { print(df[i+Offset])}
>>
>>
>> 2) use absolute values if all indices are negative.
>> for (i in -200 to -1) {print(df[abs(i)])}
>>
>> Tim
>>
>>
>>
>> -----Original Message-----
>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Peter Dalgaard
via R-help
>> Sent: Monday, April 22, 2024 10:36 AM
>> To: Rolf Turner <rolfturner using posteo.net>
>> Cc: R help project <r-help using r-project.org>; Hans W <hwborchers using gmail.com>
>> Subject: Re: [R] x[0]: Can '0' be made an allowed index in R?
>>
>> [External Email]
>>
>> Heh. Did anyone bring up negative indices yet?
>>
>> -pd
>>
>>> On 22 Apr 2024, at 10:46 , Rolf Turner <rolfturner using posteo.net> wrote:
>>>
>>>
>>> See fortunes::fortune(36).
>>>
>>> cheers,
>>>
>>> Rolf Turner
>>>
>>> --
>>> Honorary Research Fellow
>>> Department of Statistics
>>> University of Auckland
>>> Stats. Dep't. (secretaries) phone:
>>>         +64-9-373-7599 ext. 89622
>>> Home phone: +64-9-480-4619
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat/
>>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl.edu
>>> %7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
>>> %7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
>>> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=
>>> wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D&reserved=0
>>> PLEASE do read the posting guide
>>> http://www.r/
>>> -project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu%7C79
>>> ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
>>> 7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
>>> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=AP78X
>>> nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D&reserved=0
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000
Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
>>
>> ______________________________________________
>> 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.
>>
>> ______________________________________________
>> 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.
> 
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com
> 
> ______________________________________________
> 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.

______________________________________________
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