Hi all, Here's an interesting (for me, at least!) problem I came across: I have a correlation matrix, let's say with 6 variables, A to F, as column headings and the same 6 as row headings. The matrix is filled with correlation coefficients. Therefore, the diagonal is all 1's, and each of the two triangles formed by the diagonal has the same 15 correlation coefficients. I need to extract these 15 coefficients from this. I don't want the 1's and I don't want redundant values. I tried converting the matrix to a list, using c(m) and then I'm stuck. Does something like b = a[x, x+1:n] exist in R? SAS will do it. x can be looped to go from 1 to n (n here is 6, of course) and it will select the required values, but I cannot make it work. Please help. Many thanks, -DS. [[alternative HTML version deleted]]
A=matrix(1:9,3) A[lower.tri(A)] b On Nov 10, 2006, at 4:50 PM, Davendra Sohal wrote:> Hi all, > Here's an interesting (for me, at least!) problem I came across: > I have a correlation matrix, let's say with 6 variables, A to F, as > column > headings and the same 6 as row headings. > The matrix is filled with correlation coefficients. Therefore, the > diagonal > is all 1's, and each of the two triangles formed by the diagonal > has the > same 15 correlation coefficients. > I need to extract these 15 coefficients from this. I don't want the > 1's and > I don't want redundant values. > I tried converting the matrix to a list, using c(m) and then I'm > stuck. > Does something like b = a[x, x+1:n] exist in R? SAS will do it. > x can be looped to go from 1 to n (n here is 6, of course) and it will > select the required values, but I cannot make it work. > Please help. > Many thanks, > -DS. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.
try this: cor.mat <- cor(iris[1:4]) cor.mat[lower.tri(cor.mat)] I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting Davendra Sohal <dsohal at gmail.com>:> Hi all, > Here's an interesting (for me, at least!) problem I came across: > I have a correlation matrix, let's say with 6 variables, A to F, as column > headings and the same 6 as row headings. > The matrix is filled with correlation coefficients. Therefore, the diagonal > is all 1's, and each of the two triangles formed by the diagonal has the > same 15 correlation coefficients. > I need to extract these 15 coefficients from this. I don't want the 1's and > I don't want redundant values. > I tried converting the matrix to a list, using c(m) and then I'm stuck. > Does something like b = a[x, x+1:n] exist in R? SAS will do it. > x can be looped to go from 1 to n (n here is 6, of course) and it will > select the required values, but I cannot make it work. > Please help. > Many thanks, > -DS. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Look at the functions tril() and triu() in the Matrix package Regards Francisco Davendra Sohal wrote:> Hi all, > Here's an interesting (for me, at least!) problem I came across: > I have a correlation matrix, let's say with 6 variables, A to F, as column > headings and the same 6 as row headings. > The matrix is filled with correlation coefficients. Therefore, the diagonal > is all 1's, and each of the two triangles formed by the diagonal has the > same 15 correlation coefficients. > I need to extract these 15 coefficients from this. I don't want the 1's and > I don't want redundant values. > I tried converting the matrix to a list, using c(m) and then I'm stuck. > Does something like b = a[x, x+1:n] exist in R? SAS will do it. > x can be looped to go from 1 to n (n here is 6, of course) and it will > select the required values, but I cannot make it work. > Please help. > Many thanks, > -DS. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >-- Dr. Francisco J. Zagmutt College of Veterinary Medicine and Biomedical Sciences Colorado State University
"Davendra Sohal" <dsohal at gmail.com> writes:> Hi all, > Here's an interesting (for me, at least!) problem I came across: > I have a correlation matrix, let's say with 6 variables, A to F, as column > headings and the same 6 as row headings. > The matrix is filled with correlation coefficients. Therefore, the diagonal > is all 1's, and each of the two triangles formed by the diagonal has the > same 15 correlation coefficients. > I need to extract these 15 coefficients from this. I don't want the 1's and > I don't want redundant values. > I tried converting the matrix to a list, using c(m) and then I'm stuck. > Does something like b = a[x, x+1:n] exist in R? SAS will do it. > x can be looped to go from 1 to n (n here is 6, of course) and it will > select the required values, but I cannot make it work. > Please help. > Many thanks,m[lower.tri(m)] (or upper.tri(m), depending on the order in which you want the coefs) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907