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

Get R Data Analysis Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.