[R] built a lower triangular matrix from dataframe

nymphita sandrablazquezcabrera at gmail.com
Thu Feb 16 11:46:39 CET 2012


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.



More information about the R-help mailing list