O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To load the correlation matrix, run the mtcars file as shown in the following points:

  1. First, load the mtcars dataset and prepocess to rename row names and non-numeric columns:
> mtcars=read.csv("mtcars.csv")> rownames(mtcars) <- mtcars$X> mtcars$X=NULL
  1. Then generate the numerical correlation matrix, using the cor() function from the stats package, which will create correlation coefficients for each pair of columns. There are different methods for correlation analysis--pearson, spearman, and kendall--that you can pass as arguments to the method attribute of the cor function:
> mtcars_cor <- cor(mtcars, method="pearson")
  1. Next, print the value of the correlation matrix rounded up to 2 digits(digits=2):
> round(mtcars_cor, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required