I am very new to R and at times get stuck with the codes. I came across one of this code as below. What does -7 mean in the code below?
round(cor(longley[,-7]),3)
I understand: round for rounding, longley as data.frame, 3: digits for rounding, but not the -7.
Negative Indexing is used to in Python to begin slicing from the end of the string i.e. the last. Slicing in Python gets a sub-string from a string. The slicing range is set as parameters i.e. start, stop and step.
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).
Python supports “indexing from the end”, that is, negative indexing. This means the last value of a sequence has an index of -1, the second last -2, and so on. You can use negative indexing as your advantage when you want to pick values from the end (right side) of an iterable.
The negative index drops the element at the specified index position, counting from the start position. This can be used to return a set of vector values except for those which we don't want.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With