[R] Odp: Extract correlations from a matrix

Petr PIKAL petr.pikal at precheza.cz
Thu Nov 8 18:28:37 CET 2007


Hi

r-help-bounces at r-project.org napsal dne 08.11.2007 16:43:14:

> 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,

It obviusly works but you does not like the output. "w>6" results in 
logical matrix and your subset just selects appropriate values from w. You 
has to specify what output do you want especially what you want to do with 
values which do not fulfill your selection criteria.

If eg. you want them to be zero

w*(w>.6)*1

will do the trick.

if you want them to be NA

w[!(w>.6)]<-NA

will do it.

It is just the matter of what you want as an output.

Regards
Petr



> 
> 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.



More information about the R-help mailing list