[R] Vectorize a correlation matrix

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 22 12:28:38 CET 2007


Serguei Kaniovski wrote:
> Hello
>
> I can construct a correlation matrix from an (ordered) vector of
> correlation coefficients as follows:
>
> x <- c(0.1,0.2,0.3,0.4,0.5)
> n <- length(x)
> cmat <- diag(rep(0.5,n))
> cmat[lower.tri(cmat,diag=0)] <- x
> cmat <- cmat+t(cmat)
>
> But how to do the reverse operation, i.e. produce x from cmat?

This comes close:

 cmat[lower.tri(cmat)]

It produces a vector of length 10 because x was recycled in your line 4.

Duncan Murdoch



More information about the R-help mailing list