The rcorr() function from Hmisc package is used to create correlation matrix in R.

The following method shows how you can do it with syntax.

Method 1: Use rcorr() Function

library(Hmisc)

rcorr(as.matrix(df))

The following example shows how to use rcorr() function in R to create correlation matrix.

Using rcorr() Function

Let’s see how we can use rcorr() function in R:

# Import library
library(Hmisc)

# Create data frame
df <- data.frame(Pressure1=c(78.2, 88.2, 71.7, 80.21, 84.21, 82.56, 72.12, 73.85),
                 Temperature=c(35, 36, 37, 38, 32, 30, 31, 34),
                 Pressure2=c(12, 13, 11, 12, 14, 15, 13, 15))


# Create matrix of correlation coefficients and matrix of p-values
rcorr(as.matrix(df))

Output:

    Pressure1 Temperature Pressure2
Pressure1        1.00       -0.03      0.27
Temperature     -0.03        1.00     -0.71
Pressure2        0.27       -0.71      1.00

n= 8 


P
            Pressure1 Temperature Pressure2
Pressure1             0.9484      0.5242   
Temperature 0.9484                0.0467   
Pressure2   0.5242    0.0467 

As in the output it shows correlation coefficient matrix and matrix of p-value.