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]]
?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 at 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
https://cran.r-project.org/package=index0 On Sun, Apr 21, 2024, 3:56 AM Hans W <hwborchers at 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 at 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]]
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 at 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.
@vi@e@gross m@iii@g oii gm@ii@com
2024-Apr-22 01:05 UTC
[R] x[0]: Can '0' be made an allowed index in R?
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 <r-help-bounces at r-project.org> On Behalf Of Hans W Sent: Sunday, April 21, 2024 3:56 AM To: R help project <r-help at r-project.org> 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 at 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.
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 <hwborchers at 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 at 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.