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.
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
Hello,> > I'm trying to build a lower triangular matrix (with zeros in the diagonal) > from a particular dataframe. >This example constructs a lower triangular matrix from a vector, not a data.frame. (In your example, you also use a vector, the last column of the DF.) x <- runif(15) y <- matrix(0, nrow=6, ncol=6) y[lower.tri(y)] <- x # See the result y Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4391162.html Sent from the R help mailing list archive at Nabble.com.
Hi Rui, Thank you very much for your idea. It works!!! I converted my dataframe into a vector (I first removed the header and the first and second column) and then tried your solution:> data <- as.vector(as.matrix(read.table(file="data.txt", head=F, > sep="\t")[-c(1,2)])) > data[1] 5.2 9.1 8.0 2.3 8.4 6.6 7.4 7.1 5.5 4.1 3.9 9.2 8.5 7.6 9.9> y <- matrix(0, nrow=6, ncol=6) > y[upper.tri(y)] <- data > y[,1] [,2] [,3] [,4] [,5] [,6] [1,] 0 5.2 9.1 2.3 7.4 3.9 [2,] 0 0.0 8.0 8.4 7.1 9.2 [3,] 0 0.0 0.0 6.6 5.5 8.5 [4,] 0 0.0 0.0 0.0 4.1 7.6 [5,] 0 0.0 0.0 0.0 0.0 9.9 [6,] 0 0.0 0.0 0.0 0.0 0.0 Perfect! :) -- View this message in context: http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4393562.html Sent from the R help mailing list archive at Nabble.com.