[R] Extract correlations from a matrix

Christoph Scherber Christoph.Scherber at agr.uni-goettingen.de
Fri Nov 9 17:57:03 CET 2007


Dear John and the rest,

Finally, it seems that now I have found a solution for the problem:

options(width=200) #make window size bigger

#create a test dataset (which is a correlation matrix)
#with row and col names
#extract from this matrix only those correlations
#that fulfill a specific criterion (e.g. r>.6):

m=matrix(sample(1:100,replace=T),10,10)
dimnames(m)=list(letters[1:10],letters[11:20]) #define the matrix

w=cor(m,use="pairwise.complete.obs") #calculate correlations
dimnames(w)[[1]] <- letters[1:10] # (redundant)
ww <-  which(w>0.6 & w!=1,arr.ind=TRUE) #extract row and col numbers
z <- w[w>.6 & w!=1] #extract the values of w for which cor>.6

cb=cbind(ww,z)

df=data.frame(cbind(unlist(cb), #this creates the desired data frame
	rownames(m)[ww[,1]],
	colnames(m)[ww[,2]]
))

names(df)=c("rownum","colnum","cor","rowname","colname")
w
df


##see the output:
w[,7:10] #I truncated some of the first columns

        q           r           s           t
a -0.309715184 -0.09883224  0.45408912 -0.07051100
b  0.404094514  0.34563176 -0.12843155  0.35146457
c  0.002622620 -0.05056305  0.41835887  0.17600379
d  0.209332532  0.41291792 -0.34179279 -0.13641805
e  0.181234863  0.05597689 -0.54389838  0.01532882
f -0.058110523 -0.31290094 -0.04454678 -0.15160867
g  1.000000000  0.32582721 -0.64595319  0.70347824
h  0.325827210  1.00000000 -0.29604489 -0.09964339
i -0.645953195 -0.29604489  1.00000000 -0.31197856
j  0.703478239 -0.09964339 -0.31197856  1.00000000




 > df
   rownum colnum               cor rowname colname
j     10      7 0.703478238573416       j       q
g      7     10 0.703478238573416       g       t


Thanks to all who assisted in the solution of this problem!

All the best
Christoph



John Kane schrieb:
> I thought of that after I went home last night.  So
> using Rolf's suggest, will this do what you want?
> 
> Again using my simplified matrix
> 
>  m=matrix(sample(1:20,replace=T),4,5)
>  w=data.frame(cor(m,use="pairwise.complete.obs")) ;w
> ww <-  which(w>0.6 & w!=1,arr.ind=TRUE); ww
> z <- w[w>.6 & w!=1]
> 
> cbind(ww,z)
> 
> Exactly how you eliminate duplicate correlations is
> another question.
> 
> 
> --- Christoph Scherber
> <Christoph.Scherber at agr.uni-goettingen.de> wrote:
> 
>> Dear John,
>>
>> Thanks very much for your help; but actually I would
>> like to have the 
>> colNames and rowNames for the correlations
>>
>> -such that I can say: Only (a and c ) and (d and f)
>> were correlated with 
>> r>0.6:
>>
>> m=matrix(sample(1:100,replace=T),10,10)
>> dimnames(m)=list(letters[1:10],letters[11:20])
>> w=cor(m,use="pairwise.complete.obs")
>> w*(w>0.6)
>>
>> #works, but how do I get rid of those rows or
>> columns
>> #for which the colSum (or rowSum) is 1?
>>
>> Thanks very much in advance for your help!
>>
>> Best wishes
>> Christoph
>>
>>
>>
>>
>>
>> John Kane schrieb:
>>> w[w>.6] seems to work for me. I cut down the size
>> of
>>> the matrix for easier visual inspection.  
>>>
>>> m=matrix(sample(1:20,replace=T),4,5)
>>> w=cor(m,use="pairwise.complete.obs")
>>> w
>>>
>>> w[w>.6]
>>>
>>> perhaps perferabely 
>>> w[w>0.6 & w!=1] 
>>>
>>>
>>> --- Christoph Scherber
>>> <Christoph.Scherber at agr.uni-goettingen.de> wrote:
>>>
>>>> Dear R users,
>>>>
>>>> suppose I have a matrix of observations for which
>> I
>>>> calculate all 
>>>> pair-wise correlations:
>>>>
>>>> m=matrix(sample(1:100,replace=T),10,10)
>>>> w=cor(m,use="pairwise.complete.obs")
>>>>
>>>> How do I extract only those correlations that are
>>>>> 0.6?
>>>> w[w>0.6] #obviously doesn´t work,
>>>>
>>>> and I can´t find a way around it.
>>>>
>>>> I would very much appreciate any help!
>>>>
>>>> Best wishes
>>>> Christoph
>>>>
>>>>
>>>> (using R 2.5.1 on Windows XP)
>>>>
>>>>
>>>>
>>>>
>>>> -- 
>>>> Dr. Christoph Scherber
>>>> DNPW, Agroecology
>>>> University of Goettingen
>>>> Waldweg 26
>>>> D-37073 Goettingen
>>>> Germany
>>>>
>>>> phone +49(0)551 39 8807
>>>> fax   +49(0)551 39 8806
>>>> homepage www.gwdg.de/~cscherb1
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained,
>>>> reproducible code.
>>>>
>>>
>>>
>>>       Connect with friends from any web browser -
>> no download required. Try the new Yahoo! Canada
>> Messenger for the Web BETA at
>> http://ca.messenger.yahoo.com/webmessengerpromo.php
>>> .
>>>
>>
>>
> 
> 
> 
>       Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail.  Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo.ca 
> 
> .
> 



More information about the R-help mailing list