Skip to content

from table to matrix

6 messages · Carl Witthoft, Jonathan, JESSICA [via R]

#
For contour plots,  some of the contour functions in graphics packages 
(lattice for one IIRC) are pretty good at understanding that columns in 
a matrix correspond to x,y, and z values already.





********

There are many ways to do this in R. For very simple problems, this one 
is convenient:

library(ecodist)
newdata <- crosstab(mydata$x, mydata$y, mydata$z)

For more complicated problems, reshape is very powerful.

Sarah
On Tue, Dec 14, 2010 at 5:13 PM, jonathan <jon_at_than.biz> wrote:
>
 > That's so weird, I just signed up on here to ask exactly the same 
question!
 >
 > However, I think my issue is like Jessica's who says that her data is 
like
 > that, not actually that...
 >
 > So the issue is not in generating that data on-the-fly but in 
transforming
 > it from a data frame to a matrix.
 >
 > As a more concrete example, I have read the following data in from a file
 > (around 300,000 rows):
 >
 > x    y     z
 > 0    0    687
 > 0    1    64
 > 0    2    71
 > 0    3    55
 > 0    4    52
 > 0    5    51
 > 0    6    38
 > 0    7    38
 > 0    8    54
 > 0    9    49
 > .........
 > .........
 > .........
 > 304979    282977    1
 > 351377    1547980    1
 > 383835    1740541    1
 > 418133    6024710    1
 > 421549    1028572    1
 > 471314    1751836    1
 > 579602    1817393    1
 > 713515    5524385    1
 >
 >
 > So what I want to do is transform this into a matrix where at 
position (x,y)
 > in the matrix I have value z. I am doing this so that I can then do a
 > filled.contour plot on the data.
 >
 > I think this is the same as what Jessica is asking...
 >
 > Regards and many thanks,
 >
 > Jonathan
 > UCL Computer Science
#
OK well I don't mean to "hijack" Jessica's thread with a tangent on graphics
and plots, but I'm still having some trouble. I'm a total newbie here so if
the correct etiquette would be for me to start a new thread at this point
then please do advise me!!

The code I'm now trying to run is:

library(lattice)
df <- read.table("data", sep="\t", header=TRUE)

levelplot(df$z ~ df$x * df$y)

It seems to run OK but I don't seem to be getting any output... I'm running
the latest R on OS X. All the "normal" plotting functions, e.g. standard
plot() seem to work fine and display in a Quartz window, but when I run this
I'm not getting anything =/

Any tips?

Jonathan
#
Maybe I can help here:

In OS X at least you can open the R Package Installer from the Packages &
Data menu, select a mirror, etc, then type ecodist into the search bar and
then Install Selected... this will install the ecodist package on your
machine.

Not exactly sure how it would be done under other operating systems but
that's the basic idea.

Jonathan
#
I actually got it to work now using levelplot, and I also worked out how to
get the graph I wanted to display, I had to assign the graph to a reference
and then print it -- yes, that simple!

Now I just need to work out why my levelplot doesn't look quite right, even
though the data is in there... and I should be good to go!

Hope that you got your stuff to work Jessica, and thanks to everyone for
your help :-) I'm sure I will be posting here again very soon!

Jonathan