[R] built a lower triangular matrix from dataframe

nymphita sandrablazquezcabrera at gmail.com
Thu Feb 16 11:26:49 CET 2012


Hi Michael,
Your answer was very interesting, thank you!
However, I tried it and the result was:

> df <- read.table(file="df.txt", head=T, sep="\t")
> df
   i j   k
1  1 2 5.2
2  1 3 9.1
3  1 4 8.0
4  1 5 2.3
5  1 6 8.4
6  2 3 6.6
7  2 4 7.4
8  2 5 7.1
9  2 6 5.5
10 3 4 4.1
11 3 5 3.9
12 3 6 9.2
13 4 5 8.5
14 4 6 7.6
15 5 6 9.9
> x <- with(df, matrix(NA, ncol= max(j), nrow=max(i)+1))
> x[with(df, i), with(df, j)] <- with(df, k)
> x
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   NA  8.4  8.4  8.4  8.4  8.4
[2,]   NA  5.5  5.5  5.5  5.5  5.5
[3,]   NA  9.2  9.2  9.2  9.2  9.2
[4,]   NA  7.6  7.6  7.6  7.6  7.6
[5,]   NA  9.9  9.9  9.9  9.9  9.9
[6,]   NA   NA   NA   NA   NA   NA

It seems that R only reads the values [1,6], [2,6], [3,6], [4,6], and [5,6]
and repeats them for every postition of the row...
Still trying to find why and how to change it, but if someone finds a
solution it will be appreciated

--
View this message in context: http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4393528.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list