J. do
J. do

Reputation: 53

How do I use cor() for a dataframe in R?

I'm working through Introduction to Statistical Learning and I'm on Chapter 3 exercises. One of the questions is dealing with the "Auto" dataset and is asking me to compute the matrix of correlations using cor(). An added bonus is to exclude one of the columns (Names) because it's qualitative.

I tried

cor(Auto, use = "complete.obs")

but I get an error:

Error in cor(Auto, use = "complete.obs") : 'x' must be numeric

How do I do this? Thank you very much for the help.

Upvotes: 1

Views: 2055

Answers (1)

duckmayr
duckmayr

Reputation: 16910

You must read the exercises carefully!

The exercise in question states

Compute the matrix of correlations between the variables using the function cor(). You will need to exclude the name variable, which is qualitative.

So, if you just try cor(), you will get an error

cor(Auto)

Error in cor(Auto) : 'x' must be numeric

which is the error you mention. But, the authors warned you of this! So, take their recommendation and exclude the name variable:

name <- which(names(Auto) == "name")
cor(Auto[ , -name])
                    mpg  cylinders displacement horsepower     weight
mpg           1.0000000 -0.7776175   -0.8051269 -0.7784268 -0.8322442
cylinders    -0.7776175  1.0000000    0.9508233  0.8429834  0.8975273
displacement -0.8051269  0.9508233    1.0000000  0.8972570  0.9329944
horsepower   -0.7784268  0.8429834    0.8972570  1.0000000  0.8645377
weight       -0.8322442  0.8975273    0.9329944  0.8645377  1.0000000
acceleration  0.4233285 -0.5046834   -0.5438005 -0.6891955 -0.4168392
year          0.5805410 -0.3456474   -0.3698552 -0.4163615 -0.3091199
origin        0.5652088 -0.5689316   -0.6145351 -0.4551715 -0.5850054
             acceleration       year     origin
mpg             0.4233285  0.5805410  0.5652088
cylinders      -0.5046834 -0.3456474 -0.5689316
displacement   -0.5438005 -0.3698552 -0.6145351
horsepower     -0.6891955 -0.4163615 -0.4551715
weight         -0.4168392 -0.3091199 -0.5850054
acceleration    1.0000000  0.2903161  0.2127458
year            0.2903161  1.0000000  0.1815277
origin          0.2127458  0.1815277  1.0000000

Upvotes: 2

Related Questions