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.
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.
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.
Videos
In the context [, -7] it means drop the 7th column from the data frame longley (or take all columns but the 7th from longley).
This is R 101 and you'd do well to read some introductory material. For example, this is covered very early on in the An Introduction to R manual that comes with R or is accessible from the R website. Or you could read ?Extract.
Here is an example
> head(longley)
GNP.deflator GNP Unemployed Armed.Forces Population Year Employed
1947 83.0 234.289 235.6 159.0 107.608 1947 60.323
1948 88.5 259.426 232.5 145.6 108.632 1948 61.122
1949 88.2 258.054 368.2 161.6 109.773 1949 60.171
1950 89.5 284.599 335.1 165.0 110.929 1950 61.187
1951 96.2 328.975 209.9 309.9 112.075 1951 63.221
1952 98.1 346.999 193.2 359.4 113.270 1952 63.639
> names(longley)
[1] "GNP.deflator" "GNP" "Unemployed" "Armed.Forces" "Population"
[6] "Year" "Employed"
> names(longley)[7]
[1] "Employed"
> head(longley[, -7])
GNP.deflator GNP Unemployed Armed.Forces Population Year
1947 83.0 234.289 235.6 159.0 107.608 1947
1948 88.5 259.426 232.5 145.6 108.632 1948
1949 88.2 258.054 368.2 161.6 109.773 1949
1950 89.5 284.599 335.1 165.0 110.929 1950
1951 96.2 328.975 209.9 309.9 112.075 1951
1952 98.1 346.999 193.2 359.4 113.270 1952
The command longley[,-7] means: All columns from longley except the 7th. This is called negative indexing.
Have a look at ?Extract for further information.
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:
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.
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.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:
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.
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?
Very noob question, but why would you need to use a negative index over the positive one? Why would I use [-3] over just using [2] for example?
I googled this but couldn't really find an explanation anywhere