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
On Wed, 12 Mar 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?nm <- dimnames(aacor)[[1]] cbind(nm[row(aacor)[aacor<0]], nm[col(aacor)[aacor<0]]) is one way -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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>