Skip to content
Prev 257376 / 398506 Next

Data frame with 3 columns to matrix

David Winsemius <dwinsemius at comcast.net> writes:
Thanks for your answer David,

but this yields a diagonal matrix only.  I think I did not make myself
clear enough.  In the original 3 column data frame, there could have
been a pair of x and y with identical y's but different x's and z's.
The way my data source is derived, there is a guarantee that there is
are no two rows with identical x and y in the original data frame.  In
the end, x and y serve as a grid, with z values at each point in the
grid or NA's if there is no z value for a x and y pair.  The number of
rows in the data frame is then equal to the number of non-NA values in
the resulting matrix.

Another try, lets assume this original data frame:

  x  y z
1 2  5 1
2 2  6 1
3 3  7 1
4 3  8 1
5 3  9 1
6 5 10 2
7 5 11 2
8 5 12 2

Then I would like to get

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]   NA    5    6    7    8    9   10   11   12
[2,]    2    1    1           
[3,]    2
[4,]    3              1    1    1
[5,]    3
[6,]    3
[7,]    5                             2    2    2
[8,]    5
[9,]    5

I left out all the NA's, except the first, where there is no z value,
say e.g. x=5 and y=8.

Do you see what I mean?
Message-ID: <4dad7d39.628fd80a.4c94.2f83@mx.google.com>
In-Reply-To: <EDC8FC44-126A-4A3B-BAA4-FC4B2F56BE9A@comcast.net> (David Winsemius's message of "Tue, 19 Apr 2011 07:02:17 -0400")