[R] how to convert the lower triangle of a matrix to a symmetricmatrix [Broadcast]

Liaw, Andy andy_liaw at merck.com
Fri Apr 20 15:44:54 CEST 2007


Ranjan and Prof. Fox,

Similar approach can be found in stats:::as.matrix.dist().

Andy 

From: John Fox
> 
> Dear Ranjan,
> 
> If the elements are ordered by rows, then the following 
> should do the trick:
> 
>     X <- diag(p)
>     X[upper.tri(X, diag=TRUE)] <- elements
>     X <- X + t(X) - diag(diag(X))
> 
> If they are ordered by columns, substitute lower.tri() for 
> upper.tri().
> 
> I hope this helps,
>  John
> 
> --------------------------------
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario
> Canada L8S 4M4
> 905-525-9140x23604
> http://socserv.mcmaster.ca/jfox 
> -------------------------------- 
> 
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch 
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ranjan Maitra
> > Sent: Thursday, April 19, 2007 9:28 PM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] how to convert the lower triangle of a matrix to 
> > a symmetricmatrix
> > 
> > Hi,
> > 
> > I have a vector of p*(p+1)/2 elements, essentially the lower 
> > triangle of a symmetric matrix. I was wondering if there is 
> > an easy way to make it fill a symmetric matrix. I have to do 
> > it several times, hence some efficient approach would be 
> very useful.
> > 
> > Many thanks and best wishes,
> > Ranjan
> > 
> > ______________________________________________
> > 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.
> >
> 
> ______________________________________________
> 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.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}



More information about the R-help mailing list