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
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
If I understand what you want, you can use 'which' which(w>0.6, arr.ind=T) On 08/11/2007, Christoph Scherber <Christoph.Scherber@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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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. >
Hey Christoph, It is not clear what do you want to "extract". w[w>0.6] does give you the correlation values above 0.6. What is your question? Julian Christoph Scherber 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) > > > >
On 9/11/2007, at 9:01 AM, Julian Burgos wrote:> Hey Christoph, > > It is not clear what do you want to "extract". > w[w>0.6] does give you the correlation values above 0.6. What is your > question? > > Julian >Perhaps he wants which(w>0.6,arr.ind=TRUE) It is indeed hard to answer questions which require telepathy on the part of the responder. cheers, Rolf Turner> Christoph Scherber 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) >> >> >> >> > > ______________________________________________ > 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.###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################
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 > > . >