[R] variables in a correlation matrix

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Mar 12 17:55:41 CET 2003



juli g. pausas wrote:
> Hi,
> Given a correlation matrix, how can I know which variables have certain 
> correlation values?
> 
> aa <- matrix( rnorm(25, 1,1), 5,5)
> colnames(aa) <- c("v1", "v2", "v3", "v4", "v5")
> aacor <- cor(aa)
> 
> For instance, which variables are negatively correlated?    
> aacor[aacor<0]  # provide the r values, but how I get the variables?
> 
> Any suggestion?
> Thanks in advance
> 
> Juli
> 


Juli,
   How about?

R> aa <- matrix( rnorm(25, 1,1), 5,5)
R> colnames(aa) <- c("v1", "v2", "v3", "v4", "v5")
R> aacor <- cor(aa)
R> negcor <- which(aacor < 0 & row(aacor) < col(aacor), TRUE)
R> apply(negcor, 2, function(x, n)
  $   n[x],
  $   n = colnames(aacor))
      row  col
[1,] "v1" "v2"
[2,] "v1" "v3"
[3,] "v2" "v3"
[4,] "v2" "v4"
[5,] "v2" "v5"
[6,] "v3" "v5"
[7,] "v4" "v5"
R>



More information about the R-help mailing list