Is there a simple way to decompose the upper triangle of a correlation matrix to a linear list; For example: X Y Z X 1 2 3 Y 2 1 4 Z 3 4 1 so you get a list like: xy 2 XZ 3 YZ 4 I suspect you can do it with a matrix transformation, but that beyond me at present. Many thanks Mark _________________________ Department of Molecular and Human Genetics, Baylor College of Medicine,
On Thu, 23 Sep 2004 13:08:53 -0500, "Mark Strivens" <strivens at bcm.tmc.edu> wrote :>Is there a simple way to decompose the upper triangle >of a correlation matrix to a linear list; > >For example: > > X Y Z >X 1 2 3 >Y 2 1 4 >Z 3 4 1 > >so you get a list like: > >xy 2 >XZ 3 >YZ 4 > >I suspect you can do it with a matrix transformation, but >that beyond me at present.x[ row(x) < col(x) ] will give the entries you want. Duncan Murdoch
This may not have things in the order you want but you can see if it gets close to what you want:> x <- matrix(1:16,ncol=4) > x[,1] [,2] [,3] [,4] [1,] 1 5 9 13 [2,] 2 6 10 14 [3,] 3 7 11 15 [4,] 4 8 12 16> y <- x[row(x) < col(x)] > y[1] 5 9 10 13 14 15>bob -----Original Message----- From: Mark Strivens [mailto:strivens at bcm.tmc.edu] Sent: Thursday, September 23, 2004 2:09 PM To: r-help at stat.math.ethz.ch Subject: [R] decompose a correlation matrix Is there a simple way to decompose the upper triangle of a correlation matrix to a linear list; For example: X Y Z X 1 2 3 Y 2 1 4 Z 3 4 1 so you get a list like: xy 2 XZ 3 YZ 4 I suspect you can do it with a matrix transformation, but that beyond me at present. Many thanks Mark _________________________ Department of Molecular and Human Genetics, Baylor College of Medicine, ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Thanks for the answers the appear to be just right. i.e. corrmat[upper.tri(corrmat)] or x[col(x)>row(x)] The slight problem is I lose all the column & row labels from the correlation matrix, so I am not sure of the order of the values (i.e. which combination of markers the value represents). Is there anyway to drag the labels along with the value extraction? _________________________ Department of Molecular and Human Genetics, Baylor College of Medicine,
> From: Duncan Murdoch > > On Thu, 23 Sep 2004 13:08:53 -0500, "Mark Strivens" > <strivens at bcm.tmc.edu> wrote : > > >Is there a simple way to decompose the upper triangle > >of a correlation matrix to a linear list; > > > >For example: > > > > X Y Z > >X 1 2 3 > >Y 2 1 4 > >Z 3 4 1 > > > >so you get a list like: > > > >xy 2 > >XZ 3 > >YZ 4 > > > >I suspect you can do it with a matrix transformation, but > >that beyond me at present. > > x[ row(x) < col(x) ] > > will give the entries you want. > > Duncan MurdochPerhaps slightly more transparent: x[upper.tri(x)] or x[lower.tri(x)] Best, Andy
> From: Mark Strivens > > Thanks for the answers the appear to be just > right. > > i.e. > > corrmat[upper.tri(corrmat)] > > or > > x[col(x)>row(x)] > > The slight problem is I lose all the column & > row labels from the correlation matrix, so I > am not sure of the order of the values (i.e. > which combination of markers the value > represents). > > Is there anyway to drag the labels along with > the value extraction? > _________________________ > Department of Molecular and Human Genetics, > Baylor College of Medicine,idx <- upper.tri(x) cbind(row=row(x)[idx], col=col(x)[idx], value=x[idx]) Best, Andy
On 23-Sep-04 Mark Strivens wrote:> Is there a simple way to decompose the upper triangle > of a correlation matrix to a linear list; > > For example: > > X Y Z > X 1 2 3 > Y 2 1 4 > Z 3 4 1 > > so you get a list like: > > xy 2 > XZ 3 > YZ 4 > > I suspect you can do it with a matrix transformation, but > that beyond me at present.Let C be your correlation matrix. Then: C[lower.tri(C)] or equivalently C[upper.tri(C)] E.g. (in your notation): > C [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 2 1 5 6 [3,] 3 5 1 7 [4,] 4 6 7 1 > (C[lower.tri(C)]) [1] 2 3 4 5 6 7 See ?lower.tri Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 23-Sep-04 Time: 20:37:36 ------------------------------ XFMail ------------------------------
Thanks guys for the help, here's the final (grizzly?) solution: #generate a correlation matrix cm<-cor(someDataFrame, y = NULL ...) # get the list of labels (included in the dataframe) labels<-labels(cm)[[1]] # retrieve the uppper portion of the correlation matrix (as logical values) idx <- upper.tri(cm) # combine the logical values with the marker list mcm<-cbind(marker=paste((markers[col(t(cm))[idx]],'&',markers[row(t(cm))[idx ]]), row=row(t(cm))[idx], col=col(t(cm))[idx], value=cm[idx]) gives the format: label2 & label1 1 2 0.97712 label3 & label1 1 3 0.84587 label4 & label1 1 4 0.92184 label5 & label1 1 5 0.54698 There seemed to be some issues with the row()and col() commands not thinking the output of cor() was a matrix so I used t() to force it into a table... _________________________ Department of Molecular and Human Genetics, Baylor College of Medicine, 1 Baylor Plaza, Houston, TX 77030. 713-798-1947 (work) 832-876-7956 (cell)