Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, what does a negative index do?

I am porting part of a program (not enough to compile and run) from R to C++. I am not familiar with R. I have done okay using the references online, but was stumped by the following line:

cnt2.2<-cnt2[,-1]

I am guessing:

  1. cnt2 is a 2 dimensional matrix
  2. cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be.
  3. <- is an assignment.
  4. [,-1] accesses part of the array. I thought [,5] meant all rows, 5th column only. If this is correct, I have no idea what -1 refers to.
like image 606
Brad Avatar asked Sep 07 '11 15:09

Brad


People also ask

What does a negative index do?

Negative indices are powers (also called exponents) with a minus sign in front of them. E.g. We get negative indices by dividing two terms with the same base where the first term is raised to a power that is smaller than the power that the second term is raised to.

Can R hold negative index?

In the R programming language, you can use a negative index in order to exclude an element from a list or a row from a matrix.

What is the use of negative indices in slicing?

Using a negative number as an index in the slice method is called Negative slicing in Python. It returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).

What does index mean in R?

An important aspect of working with R objects is knowing how to “index” them Indexing means selecting a subset of the elements in order to use them in further analysis or possibly change them Here we focus just on three kinds of vector indexing: positional, named reference, and logical Any of these indexing techniques ...


3 Answers

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.

like image 79
Chase Avatar answered Oct 05 '22 19:10

Chase


Negative indices specify dropping (rather than retaining) particular elements ... so x[,-1] specifies dropping the first column (rows are the first dimension, before the comma, and columns are the second dimension, after the comma). From ?"[" ( http://stat.ethz.ch/R-manual/R-devel/library/base/html/Extract.html ):

For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical vectors, indicating elements/slices to select. Such vectors are recycled if necessary to match the corresponding extent. ‘i’, ‘j’, ‘...’ can also be negative integers, indicating elements/slices to leave out of the selection.

like image 24
Ben Bolker Avatar answered Oct 05 '22 19:10

Ben Bolker


1) cnt2 is a 2 dimensional matrix

From the code you provided it is indeed a 2-dimensional structure of some sort (quite possibly a matrix).

2) cnt2.2 is a new variable being declared with a period '.' used the same way an alphabetic character would be.

Correct.

3) <- is an assignment.

Correct.

4) [,-1] accesses part of the array. I thought [,5] meant all rows, 5th column only. If this is correct, I have no idea what -1 refers to.

[,-1] selects all columns except column 1. Note that, unlike in C++, indices in R start from one rather than zero.

like image 41
NPE Avatar answered Oct 05 '22 17:10

NPE