To load the correlation matrix, run the mtcars file as shown in the following points:
- 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
- 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")
- Next, print the value of the correlation matrix rounded up to 2 digits(digits=2):
> round(mtcars_cor, ...