Message-ID: <CAAmySGOxXiNYjLopM8Uo75NcOMgaXLS945dWWVnwxCJAsQHOKA@mail.gmail.com>
Date: 2012-02-16T20:54:07Z
From: R. Michael Weylandt
Subject: built a lower triangular matrix from dataframe
In-Reply-To: <1329388009533-4393528.post@n4.nabble.com>
I didn't think through mine all the way -- you do need the cbind()
call to do the indexing like I was thinking -- so mine when corrected
just turns into David's.
Michael
On Thu, Feb 16, 2012 at 5:26 AM, nymphita
<sandrablazquezcabrera at gmail.com> wrote:
> 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.
>
> ______________________________________________
> 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.