| Title: | Zero-Based Indexing in R |
|---|---|
| Description: | Extract and replace elements using indices that start from zero (rather than one), as is common in mathematical notation and other programming languages. |
| Authors: | David Antony Selby [aut, cre] (ORCID: <https://orcid.org/0000-0001-8026-5663>) |
| Maintainer: | David Antony Selby <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.3.9000 |
| Built: | 2026-06-09 08:36:49 UTC |
| Source: | https://github.com/selbosh/index0 |
When combining vectors, if the first argument to c() is zero-indexed, then
the result will be zero-indexed as well.
Otherwise, the output will revert to default R behaviour of indexing from 1.
## S3 method for class 'index0' c(...)## S3 method for class 'index0' c(...)
... |
objects to be concatenated. All |
A zero-indexed vector of class index0.
x <- as.index0(1:5) y <- as.index0(6:10) c(x, y) c(1:5, y)x <- as.index0(1:5) y <- as.index0(6:10) c(x, y) c(1:5, y)
Works like utils::head() and utils::tail().
## S3 method for class 'index0' head(x, ...) ## S3 method for class 'index0' tail(x, ...)## S3 method for class 'index0' head(x, ...) ## S3 method for class 'index0' tail(x, ...)
x |
An |
... |
Other arguments, passed to generic function |
Just because an object is zero-indexed, doesn't mean that the definition of,
for example, "the first 5 elements" or "the last two elements" has changed.
Thus we add methods head() and tail() to ensure they behave as normal.
An index0 object
Normally R is indexed from 1, but with the special index0 class, you can
have vectors that are indexed from zero. Works both for subsetting (extraction)
and (sub-)assignment.
An index0 object is just like a normal vector or matrix, but x[i] returns
or replaces the (i+1)th index.
## S3 method for class 'index0' x[i, j, ...] ## S3 replacement method for class 'index0' x[i, j, ...] <- value as.index0(x) as.index1(x) is.index0(x) index_from_0(x)## S3 method for class 'index0' x[i, j, ...] ## S3 replacement method for class 'index0' x[i, j, ...] <- value as.index0(x) as.index1(x) is.index0(x) index_from_0(x)
x |
object from which to extract element(s) or in which to replace element(s) |
i, j
|
indices specifying elements to extract or replace. Starting from 1. |
... |
other arguments passed to generic methods. |
value |
typically an array-like R object of a similar class as |
Assign the class index0 to a vector, using as.index0() or index_from_0(),
then use the subset operators normally and they will be indexed from zero.
You can reverse the operation (reset to indexing from 1) with as.index1()
or by manually removing the index0 class.
Character indices seem to be unaffected. Be cautious with logical indices.
See examples.
as.index0 returns the input (typically a vector or matrix) unchanged except
for the addition of an index0 class attribute, which enables the zero-based
indexing behaviour. Use as.index1 to remove this class again, if present.
If x is a zero-indexed object with class index0, then x[i] returns an
appropriate subset of x. The returned subset is also zero-indexed.
x[i] <- value changes the ith element (effectively (i+1)th element in
ordinary R code) in place.
is.index0(x) returns TRUE if x is indexed from zero, otherwise FALSE.
Partially inspired by this Stack Overflow answer: Zero based arrays/vectors in R
# Vectors v <- as.index0(letters) v[0:3] v[c(0, 2)] <- c('zeroth', 'second') v # Matrices and arrays m <- index_from_0(matrix(1:4, 2)) m[0, 1] m[0, 1] <- 99 m# Vectors v <- as.index0(letters) v[0:3] v[c(0, 2)] <- c('zeroth', 'second') v # Matrices and arrays m <- index_from_0(matrix(1:4, 2)) m[0, 1] m[0, 1] <- 99 m
When printing zero-indexed objects, it only seems fair that the printed output is zero-indexed as well. So we replace those little numbers in square brackets so that they start from zero.
## S3 method for class 'index0' print(x, ...) ## S3 method for class 'index0' str(object, ...)## S3 method for class 'index0' print(x, ...) ## S3 method for class 'index0' str(object, ...)
x, object
|
An object to inspect/print, of class |
... |
Other arguments, passed to generic function |
Not yet implemented for matrices, arrays or data frames.