Dear R users, I am not good in R-language programming. So, i need your help. I want to convert my lower-triangle value of symmetry matrix into a vector with their row and column name. I found a function called "sm2vec" in "corpcor" package but it give only a vector of values but not row and column names. But i also want ROW and COLUMN name together with their corresponding values. To make more clear, For example: Suppose i have a symmetric matrix with lower-triangle value with 5 variables (eg. a - e). But in real, i have many variables. a b c d e a - - - - - b -0.44145 - - - - c -0.67922 0.232548 - - - d 0.114012 0.186583 0.244655 - - e -0.031 -0.35098 -0.16327 -0.34573 - Now, I want to convert above table into the following format. So, if some one can tell what kind of R code does this, it will be great help. row name column name value b a -0.44145 c a -0.67922 d a 0.114012 e a -0.031 c b 0.232548 d b 0.186583 e b -0.35098 d c 0.244655 e c -0.16327 e d -0.34573 If some one can help me, it will be great for me. Thanks in advance for your help. Regards, Ram Kumar Basnet Ph. D. student. [[alternative HTML version deleted]]
Try something like this: X = matrix(rnorm(25), 5); colnames(X) <- rownames(X) <- letters[1:5] Y <- dist(X) library(reshape) melt(as.matrix(Y)) Michael On Tue, Nov 15, 2011 at 7:03 PM, ram basnet <basnetabc at yahoo.com> wrote:> Dear R users, > > I am not good in R-language programming. So, i need your help. > > I want to convert my lower-triangle value of symmetry matrix into a vector with their row and column name. > I found a function called "sm2vec" in "corpcor" package but it give only a vector of values but not row and column names. But i also want ROW and COLUMN name together with their corresponding values. > > To make more clear, > For example: Suppose i have a symmetric matrix with lower-triangle value with 5 variables (eg. a - e). But in real, i have many variables. > > ? a b c d e > a ?- - - - - > b -0.44145 ?- - - ?- > c -0.67922 0.232548 ?- - ?- > d 0.114012 0.186583 0.244655 ?- ?- > e -0.031 -0.35098 -0.16327 -0.34573 ?- > > Now, I want to convert above table into the following format. So, if some one can tell what kind of R code does this, it will be great help. > > row name column name value > b a -0.44145 > c a -0.67922 > d a 0.114012 > e a -0.031 > c b 0.232548 > d b 0.186583 > e b -0.35098 > d c 0.244655 > e c -0.16327 > e d -0.34573 > > If some one can help me, it will be great for me. > > Thanks in advance for your help. > > Regards, > Ram Kumar Basnet > Ph. D. student. > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > 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. > >
R. Michael Weylandt
2011-Nov-16 12:48 UTC
[R] Conversion of symmetry matrix into a vector ONE MORE QUESTION
Try the as.dist() function -- it won't change any values, only give you something that, upon printing, gets formatted like a dist object. as.matrix() will reverse it. Michael On Wed, Nov 16, 2011 at 5:58 AM, ram basnet <basnetabc at yahoo.com> wrote:> Dear Michael, > Thank you very much. > That what i exactly want. > > One more question: > Do you know some code that convert a symmetric matrix into a > lower-triangular matrix (not vector) ? > I tried with function "lower.tri" and "lower" (from ecodist package), but > they don't give lower-triangular value in the form of matrix, it gives in > the form of vector. > The function "dist" you used here, is not suitable to calculate correlation. > > To be more clear, i want to calculate correlation first, which give a > symmetric matrix. Now i want to convert it into a "lower-triangular > correlation matrix". > If there is some way available or some function in R, it will be very nice. > Thank you very much. > Wish you good time. > Thanks > > Regards, > Ram Kumar Basnet. > Ph. D. student > > From: R. Michael Weylandt <michael.weylandt at gmail.com> > To: ram basnet <basnetabc at yahoo.com> > Cc: R help <r-help at r-project.org> > Sent: Wednesday, November 16, 2011 5:26 AM > Subject: Re: [R] Conversion of symmetry matrix into a vector > > Try something like this: > > X = matrix(rnorm(25), 5); colnames(X) <- rownames(X) <- letters[1:5] > > Y <- dist(X) > > library(reshape) > melt(as.matrix(Y)) > > Michael > > On Tue, Nov 15, 2011 at 7:03 PM, ram basnet <basnetabc at yahoo.com> wrote: >> Dear R users, >> >> I am not good in R-language programming. So, i need your help. >> >> I want to convert my lower-triangle value of symmetry matrix into a vector >> with their row and column name. >> I found a function called "sm2vec" in "corpcor" package but it give only a >> vector of values but not row and column names. But i also want ROW and >> COLUMN name together with their corresponding values. >> >> To make more clear, >> For example: Suppose i have a symmetric matrix with lower-triangle value >> with 5 variables (eg. a - e). But in real, i have many variables. >> >> ? a b c d e >> a ?- - - - - >> b -0.44145 ?- - - ?- >> c -0.67922 0.232548 ?- - ?- >> d 0.114012 0.186583 0.244655 ?- ?- >> e -0.031 -0.35098 -0.16327 -0.34573 ?- >> >> Now, I want to convert above table into the following format. So, if some >> one can tell what kind of R code does this, it will be great help. >> >> row name column name value >> b a -0.44145 >> c a -0.67922 >> d a 0.114012 >> e a -0.031 >> c b 0.232548 >> d b 0.186583 >> e b -0.35098 >> d c 0.244655 >> e c -0.16327 >> e d -0.34573 >> >> If some one can help me, it will be great for me. >> >> Thanks in advance for your help. >> >> Regards, >> Ram Kumar Basnet >> Ph. D. student. >> ? ? ? ?[[alternative HTML version deleted]] >> >> >> ______________________________________________ >> 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. >> >> > > >