To convert a covariance matrix into a correlation matrix, you can use the cov2cor
function. The function is in the base package, so no need to install or load a separate package.
Here is an example:
set.seed(123)
x <- rnorm(50, sd=runif(30, 2, 50))
d <- matrix(x, 10)
V <- cov(d)
V
[,1] [,2] [,3] [,4] [,5]
[1,] 743.05095 -504.7858 240.966105 31.241463 190.86439
[2,] -504.78580 1149.2908 -149.312409 32.251202 -950.09657
[3,] 240.96610 -149.3124 575.844899 2.912683 -12.99241
[4,] 31.24146 32.2512 2.912683 378.453935 -131.68655
[5,] 190.86439 -950.0966 -12.992409 -131.686551 1150.75884
cor(d)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 -0.54623919 0.368378182 0.058913599 0.20640654
[2,] -0.5462392 1.00000000 -0.183538918 0.048901752 -0.82615325
[3,] 0.3683782 -0.18353892 1.000000000 0.006239272 -0.01596044
[4,] 0.0589136 0.04890175 0.006239272 1.000000000 -0.19954587
[5,] 0.2064065 -0.82615325 -0.015960436 -0.199545874 1.00000000
cov2cor(V)
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 -0.54623919 0.368378182 0.058913599 0.20640654
[2,] -0.5462392 1.00000000 -0.183538918 0.048901752 -0.82615325
[3,] 0.3683782 -0.18353892 1.000000000 0.006239272 -0.01596044
[4,] 0.0589136 0.04890175 0.006239272 1.000000000 -0.19954587
[5,] 0.2064065 -0.82615325 -0.015960436 -0.199545874 1.00000000
Using linear Algebra directly:
solve(diag(sqrt(diag(V)))) %*% V %*% solve(diag(sqrt(diag(V))))
[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 -0.54623919 0.368378182 0.058913599 0.20640654
[2,] -0.5462392 1.00000000 -0.183538918 0.048901752 -0.82615325
[3,] 0.3683782 -0.18353892 1.000000000 0.006239272 -0.01596044
[4,] 0.0589136 0.04890175 0.006239272 1.000000000 -0.19954587
[5,] 0.2064065 -0.82615325 -0.015960436 -0.199545874 1.00000000
To convert the correlation matrix back to a covariance matrix, you need the variances/SDs (the variances are the diagonal elements in the covariance matrix):
diag(sqrt(diag(V))) %*% cov2cor(V) %*% diag(sqrt(diag(V)))
[,1] [,2] [,3] [,4] [,5]
[1,] 743.05095 -504.7858 240.966105 31.241463 190.86439
[2,] -504.78580 1149.2908 -149.312409 32.251202 -950.09657
[3,] 240.96610 -149.3124 575.844899 2.912683 -12.99241
[4,] 31.24146 32.2512 2.912683 378.453935 -131.68655
[5,] 190.86439 -950.0966 -12.992409 -131.686551 1150.75884