To create covariance matrix for data frame in R, you can use cov() function.

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

Method: Use cov() Function


The following example shows how to create covariance matrix in R using cov() function.

Create Covariance Matrix for R Data Frame Using cov() Function

Let’s see how we can use create covariance matrix in R:

# Create data frame
df <- data.frame(Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))

# Create covariance matrix
matrix <- cov(df)

# Print covariance matrix


           Value Reading
Value   224.5667    42.6
Reading  42.6000   110.4

Here the output shows covariance matrix for data frame df.