To create a correlation matrix for data frame in R, you can use cor() function.
The following method shows how you can do it with syntax.
Method: Use cor() Function
cor(df)
The following example shows how create correlation matrix in R using cor() function.
Using cor() Function
Let’s see how we can use cor() function to create correlation matrix:
# Create data frame
df <- data.frame(Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))
# Correlation matrix
matrix <- cor(df)
# Print correlation matrix
print(matrix)
Output:
Value Reading
Value 1.000000 0.270553
Reading 0.270553 1.000000
As the output shows correlation matrix for Value and Reading column of data frame.