Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpretation of negative index when subsetting a data.frame [duplicate]

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.

like image 806
mani Avatar asked Jan 18 '14 21:01

mani


People also ask

What does negative indexing mean?

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.

What does a negative index mean in R?

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).

What is negative indexing Why is it needed Can you give an example for the same in Python?

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.

What will happen if we use a negative index for a vector in R programming?

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.


2 Answers

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
like image 124
Gavin Simpson Avatar answered Nov 08 '22 19:11

Gavin Simpson


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.

like image 39
Sven Hohenstein Avatar answered Nov 08 '22 21:11

Sven Hohenstein