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
wrote:
> Hi Michael,
> Your answer was very interesting, thank you!
> However, I tri
Hey,
You could also use (after initializing x):
x[lower.tri(x)] <- data$k
x <- t(x)
Cheers,
Tsjerk
On Feb 16, 2012 6:59 PM, "Rui Barradas" wrote:
Hello,
I'm glad it helped.
The difference in the ordering is due to the fact that R defaults to
column-first ordering.
David's solution uses row
Hello,
I'm glad it helped.
The difference in the ordering is due to the fact that R defaults to
column-first ordering.
David's solution uses row-first (which is what you wanted).
Rui Barradas
--
View this message in context:
http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-
Hi again,
I just realized that in this solution there is something funny on the
position the values in the matrix, they don't really correspond to the
position indicated in the subscripts... However, David Winsemius has given a
valid solution.
Thank you for all your ideas!
--
View this message i
Problem solved.
Many many thanks for your ideas!!
(this site is very stimulant) :)
--
View this message in context:
http://r.789695.n4.nabble.com/built-a-lower-triangular-matrix-from-dataframe-tp4390813p4393619.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.
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
1
Hi David,
What an good solution. It works perfectly and it's really simple.
(I only removed the "1+" in ncol=1+max(j), it already has 6 columns)
My result has been:
> 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
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 <- matri
On Feb 15, 2012, at 11:47 AM, nymphita 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 reali
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,
Michael
On Wed, Feb 15, 2012 at 11:47 AM, nymphita
wrote:
> Hi T
Sorry, I just realized that it's not a lower triangualr matrix, but an upper
triangular matrix!
But still the solution/s should be rather similar in both cases.
http://r.789695.n4.nabble.com/file/n4391127/matrix2.png
I apologize for creating confusion...
Nymphita
--
View this message in context
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 data
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 wrote:
> Hello!
>
> I'm trying to build a lower triangular matrix (with zeros in the diagonal)
> from a particular dataframe.
>
> The matrix I
14 matches
Mail list logo