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

2024-04-24 Thread Hans W
I hereby protest strongly against the misuse of this thread through
unnecessary and pointless discussions. My request was satisfactorily
answered with references to R libraries and legitimate warnings about
the use of zero-based indexing schemes in R. If you want to continue
the discussions, PLEASE open another thread for it as I receive all
these comments as unsolicited emails.

__
R-help@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.


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

2024-04-24 Thread Richard O'Keefe
Note that elt(x, -1) is NOT AN OCCURRENCE OF AN EXISTING OPERATION.
It's a call to elt(-,-), which has its own semantics.
And the semantics of (x, i) when i is outside the interval
[0,length(x)) is UNDEFINED.

# elt(x, i) where i is numeric and all(0 <= i & i < length(x)) selects
one or more
# elements from x using 0-origin elements.  i should not be logical and must not
# be character.  This cannot be used for deleting.
elt <- function (x, i) x[i+1]

# elt(x, i) <- value where i is numeric and all(0 <= i & i <
length(x)) replaces one
# or more elements of x with (corresponding elements of) value using
0-origin indexing.
# i should not be logical and must not be character.
"elt<-" <- function (x, i, value) { x[i+1] <- value; x}

The goal was to get the effect of 0-origin indexing, and that's all
elt(-,-) is defined to do.
Recall the starting point of this thread.
There is existing code in another language or other languages where
0-origin is the
norm, and it would be pleasant to transliterate it into R introducing
fewer errors.
That language/those languages almost certainly do not use R's negative-indexing-
for-deletion.  Even APL doesn't do that.  Therefore elt(-,-) need not
do that.  And it
doesn't,

And a limitation like this is useful.  If you want to change/extend
the semantics of :"[",
you have to worry about ALL the legal ways to use "[".
But if you are introducing a NEW operation you only have to worry
about the things
it's intended to do (like play the limited role that indexing plays in
some other language(s)).

On Wed, 24 Apr 2024 at 13:20, Bert Gunter  wrote:
>
> "This works with any single-index value, and lets all the existing
> operations for such values continue to work."
>
> As Peter Dalgaard already pointed out, that is false.
>
> > x <- 1:4
> > x[-1]
> [1] 2 3 4
> > elt(x,-0)
> [1] 1
>
> Cheers,
> Bert
>
> On Tue, Apr 23, 2024 at 4:55 PM Richard O'Keefe  wrote:
> >
> > Does it have to use square bracket syntax?
> > elt <- function (x, i) x[i+1]
> > "elt<-" <- function (x, i, value) { x[i+1] <- value; x }
> >
> > > u <- c("A","B","C")
> > > elt(u,0)
> > [1] "A"
> > > elt(u,length(u)-1)
> > [1] "C"
> > > elt(u,0) <- "Z"
> > > u
> > [1] "Z" "B" "C"
> >
> > This works with any single-index value, and lets all the existing
> > operations for such values continue to work.  It seems to me to be the
> > simplest and cleanest way to do things, and has the advantage of
> > highlighting to a human reader that this is NOT normal R indexing.
> >
> > On Sun, 21 Apr 2024 at 19:56, Hans W  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@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@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@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.


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

2024-04-24 Thread Richard O'Keefe
No, my claim ISN'T false,  In fact, it CANNOT be.
Here's my claim again, in slow speed.
1. The goal is to get the EFFECT of 0-origin indexing,  Not
necessarily the syntax.
2. Rule A.  NO NEW DATA TYPES.
3, Rule B.  NO CHANGES TO EXISTING OPERATIONS, INCLUDING "[" and "[<-",
>From rules A and B, it follows that EXISTING OPERATIONS (which aren't changed)
continue to work on EXISTING DATA TYPES (which aren't changed) because
NOTHING RELEVANT CHANGES.
4. Rule C.  As a consequence of rules A and B, to accomplish the goal we should
DEFINE A NEW OPERATION with a new name that does whatever we want it to.
5. Because existing code does not call the new operation (it's a new name),
it does not break.  (OK, if something depends on the absence of "elt",
that could
break.  But code that depends on the absence of variables is broken already.)
6. The specific proposal was
elt <- function (x, i) x[i+1]
"elt<-" <- function (x, i, value) { x[i+1] <- value; x }
7, with the implication that you can use any name you like
8. and the perhaps less obvious implication that these definitions can
be local to
a function or a namespace.

The idea is that instead of foobar[0] with a modified data type for foobar or a
modified definition for "[" you write elt(foobar, 0) which obviously
isn't a normal
use of "[".

If you can explain how this breaks normal indexing, pray do so.

On Wed, 24 Apr 2024 at 13:20, Bert Gunter  wrote:
>
> "This works with any single-index value, and lets all the existing
> operations for such values continue to work."
>
> As Peter Dalgaard already pointed out, that is false.
>
> > x <- 1:4
> > x[-1]
> [1] 2 3 4
> > elt(x,-0)
> [1] 1
>
> Cheers,
> Bert
>
> On Tue, Apr 23, 2024 at 4:55 PM Richard O'Keefe  wrote:
> >
> > Does it have to use square bracket syntax?
> > elt <- function (x, i) x[i+1]
> > "elt<-" <- function (x, i, value) { x[i+1] <- value; x }
> >
> > > u <- c("A","B","C")
> > > elt(u,0)
> > [1] "A"
> > > elt(u,length(u)-1)
> > [1] "C"
> > > elt(u,0) <- "Z"
> > > u
> > [1] "Z" "B" "C"
> >
> > This works with any single-index value, and lets all the existing
> > operations for such values continue to work.  It seems to me to be the
> > simplest and cleanest way to do things, and has the advantage of
> > highlighting to a human reader that this is NOT normal R indexing.
> >
> > On Sun, 21 Apr 2024 at 19:56, Hans W  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@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@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@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.


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

2024-04-23 Thread Bert Gunter
"This works with any single-index value, and lets all the existing
operations for such values continue to work."

As Peter Dalgaard already pointed out, that is false.

> x <- 1:4
> x[-1]
[1] 2 3 4
> elt(x,-0)
[1] 1

Cheers,
Bert

On Tue, Apr 23, 2024 at 4:55 PM Richard O'Keefe  wrote:
>
> Does it have to use square bracket syntax?
> elt <- function (x, i) x[i+1]
> "elt<-" <- function (x, i, value) { x[i+1] <- value; x }
>
> > u <- c("A","B","C")
> > elt(u,0)
> [1] "A"
> > elt(u,length(u)-1)
> [1] "C"
> > elt(u,0) <- "Z"
> > u
> [1] "Z" "B" "C"
>
> This works with any single-index value, and lets all the existing
> operations for such values continue to work.  It seems to me to be the
> simplest and cleanest way to do things, and has the advantage of
> highlighting to a human reader that this is NOT normal R indexing.
>
> On Sun, 21 Apr 2024 at 19:56, Hans W  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@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@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@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.


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

2024-04-23 Thread Richard O'Keefe
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  On Behalf Of John Fox
> Sent: Tuesday, April 23, 2024 10:24 AM
> To: Peter Dalgaard 
> Cc: R help project ; Hans W 
> 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,]
> 123456789   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  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  On Behalf Of Peter Dalgaard
> via R-help
> >> Sent: Monday, April 22, 2024 10:36 AM
> >> To: Rolf Turner 
> >> Cc: R help project ; Hans W 
> >> 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  wrote:
> >>>
> >>>
> >>> See fortunes::fortune(36).
> >>>
> >>> cheers,
> >>>
> >>> Rolf Turner
> >>>
> >>> --
> >>> Honorary Research Fellow
> >>> Department of Statistics
> >>

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

2024-04-23 Thread Richard O'Keefe
Does it have to use square bracket syntax?
elt <- function (x, i) x[i+1]
"elt<-" <- function (x, i, value) { x[i+1] <- value; x }

> u <- c("A","B","C")
> elt(u,0)
[1] "A"
> elt(u,length(u)-1)
[1] "C"
> elt(u,0) <- "Z"
> u
[1] "Z" "B" "C"

This works with any single-index value, and lets all the existing
operations for such values continue to work.  It seems to me to be the
simplest and cleanest way to do things, and has the advantage of
highlighting to a human reader that this is NOT normal R indexing.

On Sun, 21 Apr 2024 at 19:56, Hans W  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@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@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.


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

2024-04-23 Thread avi.e.gross
d
preserve functionality.




-Original Message-
From: R-help  On Behalf Of John Fox
Sent: Tuesday, April 23, 2024 10:24 AM
To: Peter Dalgaard 
Cc: R help project ; Hans W 
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,]
123456789   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  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  On Behalf Of Peter Dalgaard
via R-help
>> Sent: Monday, April 22, 2024 10:36 AM
>> To: Rolf Turner 
>> Cc: R help project ; Hans W 
>> 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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat/
>>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
>>> %7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
>>> %7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
>>> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
>>> wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
>>> PLEASE do read the posting guide
>>> http://www.r/
>>> -project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
>>> ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
>>> 7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
>>> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
>>> nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com
>>
>> __
>> R-help@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@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@cbs.dk  Priv: pda...@gmail.com
> 
> __
> R-help@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@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@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.


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

2024-04-23 Thread Jeff Newmiller via R-help
While I certainly think using negative indices to denote element exclusion is a 
cool feature, I think people wanting to use zero-based indexes probably are not 
planning to use that feature. Python uses negative numbers to index from the 
end, which is a completely different use of negative indexes with the exclusion 
feature that R users just happily ignore. It is only the possible surprise that 
exclusion doesn't work consistently that is relevant here.

I would hope that uses of alternate indexing schemes in most packages be 
wrapped inside functions as much as possible so that R users don't have to get 
dragged into this paradigm shift to make use of those algorithms.

On April ,
>> v
>[0,] [1,] [2,] [3,] [4,] [5,] [6,] [7,] [8,] [9,]
>   123456789   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  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  On Behalf Of Peter Dalgaard via 
>>> R-help
>>> Sent: Monday, April 22, 2024 10:36 AM
>>> To: Rolf Turner 
>>> Cc: R help project ; Hans W 
>>> 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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat/
>>>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
>>>> %7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
>>>> %7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
>>>> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
>>>> wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
>>>> PLEASE do read the posting guide
>>>> http://www.r/
>>>> -project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
>>>> ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
>>>> 7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
>>>> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
>>>> nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com
>>> 
>>> __
>>> R-help@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@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, mini

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

2024-04-23 Thread John Fox

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,]
   123456789   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  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  On Behalf Of Peter Dalgaard via 
R-help
Sent: Monday, April 22, 2024 10:36 AM
To: Rolf Turner 
Cc: R help project ; Hans W 
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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat/
.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
%7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
%7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
PLEASE do read the posting guide
http://www.r/
-project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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@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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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@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.


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

2024-04-23 Thread Peter Dalgaard via R-help
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  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  On Behalf Of Peter Dalgaard via 
> R-help
> Sent: Monday, April 22, 2024 10:36 AM
> To: Rolf Turner 
> Cc: R help project ; Hans W 
> 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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat/
>> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
>> %7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
>> %7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
>> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
>> wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
>> PLEASE do read the posting guide
>> http://www.r/
>> -project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
>> ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
>> 7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
>> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
>> nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com
> 
> __
> R-help@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@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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


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

2024-04-23 Thread Martin Maechler
> Ben Bolker 
> on Sun, 21 Apr 2024 10:23:50 -0400 writes:

> Also https://cran.r-project.org/package=Oarray (which is older and 
> hence possibly more stable)

also maintained and written by a careful and really good person.
I do recommend  Oarray  as well.

Martin

BB> On 2024-04-21 3:55 a.m., Hans W 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.

__
R-help@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.


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

2024-04-22 Thread Spencer Graves

Convert to character:


goofy <- 1:9
names(goofy) <- (-4):4
goofy[as.character(0)]
goofy["-2"]


  sg


On 4/22/24 10:31 AM, Ebert,Timothy Aaron 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  On Behalf Of Peter Dalgaard via 
R-help
Sent: Monday, April 22, 2024 10:36 AM
To: Rolf Turner 
Cc: R help project ; Hans W 
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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat/
.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
%7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
%7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
PLEASE do read the posting guide
http://www.r/
-project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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@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@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.


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

2024-04-22 Thread Ebert,Timothy Aaron
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  On Behalf Of Peter Dalgaard via 
R-help
Sent: Monday, April 22, 2024 10:36 AM
To: Rolf Turner 
Cc: R help project ; Hans W 
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  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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help=05%7C02%7Ctebert%40ufl.edu
> %7C79ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638493933686698527%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=
> wmv9OYcMES0nElT9OAKTdjBk%2BB55bQ7BjxOuaVVkPg4%3D=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html=05%7C02%7Ctebert%40ufl.edu%7C79
> ca6aadcaee4aa3241308dc62d986f6%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638493933686711061%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C=AP78X
> nfKrX6B0YVM0N76ty9v%2Fw%2BchHIytw33X7M9umE%3D=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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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@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.


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

2024-04-22 Thread Peter Dalgaard via R-help
Heh. Did anyone bring up negative indices yet?

-pd

> On 22 Apr 2024, at 10:46 , Rolf Turner  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@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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


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

2024-04-22 Thread Hans W
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,]
#>2357   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  wrote:
>
> https://cran.r-project.org/package=index0
>
> On Sun, Apr 21, 2024, 3:56 AM Hans W  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@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@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.


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

2024-04-22 Thread Rolf Turner


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@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.


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

2024-04-21 Thread avi.e.gross
Hans,

It is a good question albeit R made a conscious decision to have indices
that correspond to things like row numbers and thus start with 1. Some
others have used a start of zero but often for reasons more related to
making use of all combinations of the implementation of integers on many
machines where starting with 1 would only allow use of the 255 of the 256
combinations available in 8 bits and so on.

My solution when I needed to start with zero is simply to do things like
x[n+1] or have a small function that does an increment like x[inc(n)] that
makes very clear what is happening.

You have been given several possible ways closer to what you want and that
may work for you but may confuse anyone else ever looking at your code so I
would add some comments or documentation explaining your non-standard use.

But do note the possibility of issues with any solution if you use other
indexing methods like x[5:8] which might not be done consistently. 

And if you were using a 2-D structure like a matrix or data.frame, would
your columns also be starting with column 0 as in mydata[0,0] to get the
first item in the first row, or are columns still 1-based while rows are
not?

Beware some solutions may be incomplete and may result in subtle errors.
Just routinely adding 1 seems safer as you know what you will get.


-Original Message-
From: R-help  On Behalf Of Hans W
Sent: Sunday, April 21, 2024 3:56 AM
To: R help project 
Subject: [R] x[0]: Can '0' be made an allowed index in R?

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@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@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.


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

2024-04-21 Thread Ben Bolker
  Also https://cran.r-project.org/package=Oarray (which is older and 
hence possibly more stable)


On 2024-04-21 3:55 a.m., Hans W 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@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@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.


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

2024-04-21 Thread Ben Bolker
https://cran.r-project.org/package=index0

On Sun, Apr 21, 2024, 3:56 AM Hans W  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@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


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

2024-04-21 Thread Rui Barradas

Às 09:08 de 21/04/2024, Rui Barradas escreveu:

Às 08:55 de 21/04/2024, Hans W escreveu:

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@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.

Hello,

I find what you are asking awkward but it can be done with S3 classes.
Write an extraction method for the new class and in the use case below 
it works. The method increments the ndex before calling NextMethod, the 
usual extraction function.



`[.zerobased` <- function(x, i, ...) {
   i <- i + 1L
   NextMethod()
}
as_zerobased <- function(x) {
   class(x) <- c("zerobased", class(x))
   x
}

x <- 1:10
y <- as_zerobased(x)

y[0]
#> [1] 1
y[1]
#> [1] 2
y[9]
#> [1] 10
y[10]
#> [1] NA


Hope this helps,

Rui Barradas



Sorry, forgot to also define a `[[zerobased` method. It's probably safer.


`[[.zerobased` <- function(x, i, ...) {
  i <- i + 1L
  NextMethod()
}


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
R-help@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.


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

2024-04-21 Thread Rui Barradas

Às 08:55 de 21/04/2024, Hans W escreveu:

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@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.

Hello,

I find what you are asking awkward but it can be done with S3 classes.
Write an extraction method for the new class and in the use case below 
it works. The method increments the ndex before calling NextMethod, the 
usual extraction function.



`[.zerobased` <- function(x, i, ...) {
  i <- i + 1L
  NextMethod()
}
as_zerobased <- function(x) {
  class(x) <- c("zerobased", class(x))
  x
}

x <- 1:10
y <- as_zerobased(x)

y[0]
#> [1] 1
y[1]
#> [1] 2
y[9]
#> [1] 10
y[10]
#> [1] NA


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
R-help@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] x[0]: Can '0' be made an allowed index in R?

2024-04-21 Thread Hans W
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@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.