[R] built a lower triangular matrix from dataframe

Tsjerk Wassenaar tsjerkw at gmail.com
Wed Feb 15 17:18:22 CET 2012


Hi Nymphita,

?upper.tri

x <- as.data.frame(matrix(1:6,6,6))
x[upper.tri(x,diag=TRUE)] <- 0
x

Cheers,

Tsjerk

On Wed, Feb 15, 2012 at 4:33 PM, nymphita <nymphita at gmail.com> wrote:
> Hello!
>
> I'm trying to build a lower triangular matrix (with zeros in the diagonal)
> from a particular dataframe.
>
> The matrix I have to construct has 203 rows and 203 columns and that makes
> 20503 values to be included within (that's why I can't do it manually).
>
> To illustrate the dataframe I have, I'll give you an example of a dataframe
> and matrix with dimensions 6x6 (to make it shorter!)
>
> My dataframe looks more or less like this (but longeeeer):
> (i= number of row, j=number of column, k=value to be included in the matrix)
>
> http://r.789695.n4.nabble.com/file/n4390813/df.png
>
> An the matrix I should look like this:
>
> http://r.789695.n4.nabble.com/file/n4390813/matrix.png
>
> Can anyone help me about how to do it?
>
> I'm a new R user, and I've tried several combinations of diag(),
> lower.tri(), matrix(), etc. without any luck... and I don't know if I'm
> unaware of a command that can work this out.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4390813.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



-- 
Tsjerk A. Wassenaar, Ph.D.

post-doctoral researcher
Molecular Dynamics Group
* Groningen Institute for Biomolecular Research and Biotechnology
* Zernike Institute for Advanced Materials
University of Groningen
The Netherlands



More information about the R-help mailing list