This is covered in section 2.7 of the manual: http://cran.r-project.org/doc/manuals/R-intro.html#Index-vectors

It is a negative index into the cnt2 object specifying all rows and all columns except the first column.

Answer from Chase on Stack Overflow
🌐
ProjectPro
projectpro.io › recipes › access-elements-of-vector-by-negative-indexing-r
How to access elements of a vector by negative indexing in R? -
June 5, 2022 - MACHINE LEARNING RECIPES DATA CLEANING ... by negative indexing in R? The negative index drops the element at the specified index position, counting from the start position....
🌐
R-bloggers
r-bloggers.com › r bloggers › small gotcha when using negative indexing
Small gotcha when using negative indexing | R-bloggers
May 27, 2021 - Negative indexing is a commonly used method in R to drop elements from a vector or rows/columns from a matrix that the user does not want. For example, the code below drops the third column from the matrix M: Now, … Continue reading →
🌐
Wordpress
statisticaloddsandends.wordpress.com › 2021 › 05 › 27 › small-gotcha-when-using-negative-indexing
Small gotcha when using negative indexing | Statistical Odds & Ends
May 27, 2021 - Negative indexing is a commonly used method in R to drop elements from a vector or rows/columns from a matrix that the user does not want. For example, the code below drops the third column from the matrix M: Now, let's say we want to write ...
🌐
Cookbook-r
cookbook-r.com › Basics › Indexing_into_a_data_structure
Indexing into a data structure
Unlike in some other programming languages, when you use negative numbers for indexing in R, it doesn’t mean to index backward from the end.
🌐
ETH Zurich
stat.ethz.ch › pipermail › r-help › 2008-November › 179703.html
[R] what does negative indexing in a matrix mean?
> > > > I am using the RWeka and this evaluate classifier does not work on new > data like this > > > > e <- evaluate_Weka_classifier(m1235,newdata=XW4, complexity = > FALSE,class = FALSE) > > > > while it work with negative indexing: > > > > e <- evaluate_Weka_classifier(m1235,newdata=XW4[,-2], complexity = > FALSE,class = FALSE) > > > > Although I do not understand what negative indexing of a matrix means, > or whether it produces any transformation. > > > > Regards, > > > > Itziar Frades > > > > > > This e-mail is from CIC bioGUNE. The e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.
🌐
Csu-r
csu-r.github.io › Module1 › indexing.html
5.5 Indexing | R Module 1
Sometimes it’s easier to specify which columns or rows should be excluded from indexing, rather than those that should be included. To select every column except the first one, you can use a negative index:
🌐
R Tutor
r-tutor.com › r-introduction › vector › vector-index
Vector Index | R Tutorial
Unlike other programming languages, ... a vector slice containing a single member "cc". If the index is negative, it would strip the member whose position has the same absolute value as the negative index....
Find elsewhere
🌐
Google Groups
groups.google.com › g › julia-dev › c › QMSgkl-gTnM
Indexing with negative numbers as in R
I'm just saying that it shouldn't be done using negative index values. One possibility would be to use A[:,!k]. Currently !Int doesn't mean anything, and this plays nicely with logical indexing since writing !x where x is a vector of logicals inverts the set of selected indices. !Int would have to return some kind of InvertedIndex object or something like that.
🌐
Andyteucher
andyteucher.ca › rcourse_site › 04-subsetting.html
Subsetting data
In many programming languages (C and python, for example), the first element of a vector has an index of 0. In R, the first element is 1. If we use a negative number as the index of a vector, R will return every element except for the one specified:
🌐
YouTube
youtube.com › mind your exam
Negative Indexing in R Vectors - YouTube
For Online Tuitions, email at mindyourexamchannel@gmail.comNegative Index Values in R Vectors#indexing #vectors #negativeindexing #negativeindexinginr #rvect...
Published   June 14, 2023
Views   148
🌐
GitHub
github.com › JuliaLang › julia › issues › 28276
Negative indexing like R · Issue #28276 · JuliaLang/julia
February 14, 2018 - I like R's feature of selecting all but a certain element with negative indexing, e.g. > a = c(1, 2, 3) > a[-1] [1] 2 3 In Julia, this thread suggests 1:end .!= 1, which works. I think it could be ...
Published   Jul 25, 2018
🌐
R for healthcare
rforhealthcare.org › subscripting-and-subsetting
Subscripting and subsetting – R for healthcare
March 6, 2019 - Negative indexing can be used to select all of the elements of y not included in the subscript. This is done by placing a minus sign in front of the index vector. ... Subscripting of matrices is similar to the subscripting of vectors except ...
🌐
Reddit
reddit.com › r/programminglanguages › negative/signed integer indexing: yay or nay?
r/ProgrammingLanguages on Reddit: Negative/signed integer indexing: yay or nay?
September 13, 2021 -

Inko currently allows you to use negative/signed indexes for arrays and byte arrays. When used, indexing starts at the end. For example:

let numbers = Array.new(10, 20, 30)

numbers[-1] # => 30
numbers[-2] # => 20

I originally implemented this without much thought: Ruby did it, it was sometimes useful there, so I copied it.

Signed indexes bring some trouble though:

  1. You can only represent indexes up to (263)-1. Now I think most programs won't store that many values in a single collection, but it's something one has to keep in mind.

  2. You need to convert the signed indexes into unsigned indexes. This requires something like ((index % length) + length) % length, which is a fair number of instructions.

  3. I have doubts about how useful it really is.

Problem one and two come down to the same: complexity. For example, for Inko this comes in two pieces:

  1. A function that takes a signed index and an unsigned length, and converts the index to an unsigned index. My function does support collections larger that (263)-1 by upcasting the length and index to an i128, but the index size is still limited to (263)-1.

  2. A function to implement the modulo operator (not the remainder). Rust doesn't provide one (it uses remainder), so I had to write my own.

While this is hidden from the user, I do have to deal with it, and like most the less code I have to deal with the better.

Problem 3 is more subjective. The most commonly used negative index is simply -1 to get the last value. This however can be handled by introducing a dedicated last() function. I actually think this is more clear if one isn't used to signed indexes. Besides that, indexes like -2, -3, etc are rarely used in my experience. For example, in the GitLab Rails source code I can only find 37 instances of -1 being used, 11 instances of -2, and only one instance of -3.

With this in mind, I'm starting to think signed indexes aren't really worth the trouble. If you really need to index from the back, you can just do thing[thing.length - N].

What are the thoughts of the subreddit on this matter? Does your language support signed indexes? Or am I not too far off wanting to remove support for this?

Top answer
1 of 12
12
There's a better alternative that doesn't seem to be considered often. Let the language provide a type FromEnd that contains a single integer. Let the language provide a value END that contains the integer 0 (maybe use the token $ for this if your language likes that idea). Let the indexing operator be overloaded, such that it can take a value of type integer or a value of type FromEnd. That way, you can type numbers[END-1] == 30 (or numbers[$-1] == 30 perhaps). This avoids the need to repeat the array name, which I find gets very awkward. Also, you can support END-0, which is impossible without the overload.
2 of 12
12
You can only represent indexes up to 263-1. Now I think most programs won't store that many values in a single collection, but it's something one has to keep in mind. Really? If your machine only had ONE task, ONE collection, and that had elements occupying only ONE byte each, then you'd need 8 exabytes of storage for a regular non-sparse array to get to the limits of a 63-bit index. That's about the amount of RAM on a billion PCs. You need to convert the signed indexes into unsigned indexes. This requires something like ((index % length) + length) % length, which is a fair number of instructions. Why would you need to do that? Address calculations on typical PCs don't need any such conversions. If I write x := A[i], where A is a 1-based array of int, x and i are both signed ints, then the generated code might be just one instruction (R.x and R.i are registers): mov R.x, [R.i*8+A-8] What are the thoughts of the subreddit on this matter? Does your language support signed indexes? Or am I not too far off wanting to remove support for this? I have a default integer type which is 64 bits signed. It can be used for everything: storing positive and negative values; array indexing for all practical sizes; memory and object sizes; file and volume sizes; and file offsets, which can be negative. My arrays can anyway be N-based, which means the lower bound can be a negative value, example: [-16384 .. +16383]int counts This array can be indexed by all possible values of i16 (a signed 16-bit type).
🌐
GitHub
github.com › r-spatial › stars › issues › 184
Negative index values in subset ([) do not work · Issue #184 · r-spatial/stars
November 12, 2018 - In R, one can use negative values in the indexing values to remove data from an array, e.g: array(1:3, c(2,4))[1,-2] This is not possible in stars: tif = system.file("tif/L7_ETMs.tif", package = "stars") x = read_stars(tif) x[,,,-5] # Error ...
Published   Jun 27, 2019
🌐
Carnegie Mellon University
stat.cmu.edu › ~ryantibs › statcomp-F18 › lectures › iteration.html
Indexing and Iteration
Use names() for vectors and lists, and rownames(), colnames() for matrices and data frames · The most transparent way. Can index with an integer, or integer vector (or negative integer, or negative integer vector).
🌐
Annakrystalli
annakrystalli.me › rrresearchACCE20 › indexing-and-subsetting.html
Indexing and subsetting | Reproducible Research Data and Project Management in R
If we use a negative number as the index of a vector, R will return every element except for the one specified: ... In general, be aware that the result of subsetting using indices could change if the vector is reordered.
🌐
McMaster University
ms.mcmaster.ca › ~bolker › misc › brackets.html
indexing in R: why you should use [[ more and [, $ less
Another reason why you should use data.frame() rather than cbind() in general to combine things column-wise (cbind() will automatically coerce all of your data to the most general type: m0 <- matrix(1, nrow = 3, ncol = 2) cbind(m0, "a") ## "a" is automatically recycled · ## [,1] [,2] [,3] ## [1,] "1" "1" "a" ## [2,] "1" "1" "a" ## [3,] "1" "1" "a" ... t1 <- tibble(a = 1:3, b = 2:4) t2 <- tibble(c = LETTERS[1:3]) ## combines these but result is a data frame, not a tibble data.frame(t1, t2) ... Negative indices can be convenient for dropping elements, but not always (Inferno 8.1.11).