Interpreting a negative index with a subset of data.frame

I am very new to R and get stuck with codes from time to time. I came across one of these codes as shown below. What does -7 mean in the code below?

 round(cor(longley[,-7]),3) 

I understand: round for rounding, longley as data.frame, 3: numbers for rounding, but not -7.

+6
source share
2 answers

In the context of [, -7] this means the fall of the 7 th column from the longley data longley (or take all columns, but 7 th from longley ).

This is R 101, and you should read some introductory materials. For example, this is very soon described in the Introduction to R manual , which comes with R or is available from the R. website. Or can you 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 
+11
source

The longley[,-7] command means: All columns from longley except the 7th. This is called negative indexing.

Take a look at ?Extract for more information.

+1
source

All Articles