[R] built a lower triangular matrix from dataframe

R. Michael Weylandt michael.weylandt at gmail.com
Wed Feb 15 18:01:47 CET 2012

Perhaps ignore the lower-triangularity for a moment and do something like this:

x <- matrix(NA, ncol = max(j), nrow = max(i))
x[i, j] <- k

Your code will be clearer if you use with() rather than df$i constructs.

Hope this helps,


On Wed, Feb 15, 2012 at 11:47 AM, nymphita
<sandrablazquezcabrera at gmail.com> wrote:
> Hi Tsjerk!
> Thanks for your quick reply!
> It's a nice way to built a lower triangular matrix with zeros in the
> diagonal, but what I can't work out is *how to include the values of the
> third column of the dataframe inside the matrix*.
> I just realized that I forgot to explain something about the dataframe  (the
> meaning of i, j ,k) in my post:
> The data frame has three columns, the first one (i) corresponds to the "row
> subscript" of the matrix [i, ], the second one (j) corresponds to the
> "column subscript" of the matrix [ ,j], and the third column in the
> dataframe (k) is the value that has to be in the matrix (in position [i,
> j]).
> http://r.789695.n4.nabble.com/file/n4391099/df.png
> The result is that the dataframe gives you a position and a value in a lower
> triangular matrix:
> http://r.789695.n4.nabble.com/file/n4391099/matrix.png
> Still can't find a solution to built a lower triangular matrix with the
> specific values of that dataframe...
> Any more ideas, please?
> --
> View this message in context: http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4391099.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.

More information about the R-help mailing list