I have a table like this: Date TIME Q A a 1 A b 2 A c 3 B a 4 B b 5 B c 6 C a 7 C b 8 C c 9 I want use R language to turn it to a matrix like : a b c A 1 2 3 B 4 5 6 C 7 8 9 I am new to R , anyone can help? Thanks in advance! have a table like this: Date TIME Q A a 1 A b 2 A c 3 B a 4 B b 5 B c 6 ______________________________________ This email was sent by JESSICA (via Nabble) Your replies will appear at http://r.789695.n4.nabble.com/from-table-to-matrix-tp3087972p3087972.html To receive all replies by email, subscribe to this discussion: http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=subscribe_by_code&node=3087972&code=ci1oZWxwQHItcHJvamVjdC5vcmd8MzA4Nzk3MnwtNzg0MjM1NTA4 [[alternative HTML version deleted]]
Here's one way:> df = data.frame(Date=rep(LETTERS[1:3],each=3),TIME=rep(letters[1:3],3),Q=1:9) > dfDate TIME Q 1 A a 1 2 A b 2 3 A c 3 4 B a 4 5 B b 5 6 B c 6 7 C a 7 8 C b 8 9 C c 9> mat = matrix(0,3,3,dimnames=list(LETTERS[1:3],letters[1:3])) > mat[as.matrix(df[,1:2])] = df[,3] > mata b c A 1 2 3 B 4 5 6 C 7 8 9 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 14 Dec 2010, JESSICA [via R] wrote:> > I have a table like this: > > Date TIME Q > > A a 1 > A b 2 > A c 3 > B a 4 > B b 5 > B c 6 > C a 7 > C b 8 > C c 9 > > I want use R language to turn it to a matrix like : > a b c > A 1 2 3 > B 4 5 6 > C 7 8 9 > > > I am new to R , anyone can help? Thanks in advance! have a table like this: > > Date TIME Q > > A a 1 > A b 2 > A c 3 > B a 4 > B b 5 > B c 6 > > > ______________________________________ > This email was sent by JESSICA (via Nabble) > Your replies will appear at http://r.789695.n4.nabble.com/from-table-to-matrix-tp3087972p3087972.html > To receive all replies by email, subscribe to this discussion: http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=subscribe_by_code&node=3087972&code=ci1oZWxwQHItcHJvamVjdC5vcmd8MzA4Nzk3MnwtNzg0MjM1NTA4 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
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 ______________________________________________ R-help@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. ______________________________________ View message @ http://r.789695.n4.nabble.com/from-table-to-matrix-tp3087972p3088156.html To unsubscribe from from table to matrix, visit http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3087972&code=ci1oZWxwQHItcHJvamVjdC5vcmd8MzA4Nzk3MnwtNzg0MjM1NTA4 [[alternative HTML version deleted]]
Thank you guys I learnt a lot . But when I tried to run the library(ecodist) function, R says there is no package called "ecodist". why? ______________________________________ View message @ http://r.789695.n4.nabble.com/from-table-to-matrix-tp3087972p3088184.html To unsubscribe from from table to matrix, visit http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3087972&code=ci1oZWxwQHItcHJvamVjdC5vcmd8MzA4Nzk3MnwtNzg0MjM1NTA4 [[alternative HTML version deleted]]