Dear R-helper, I have a data like: > hec.data <-array(c(5,15,20,68,29,54,84,119,14,14,17,26,16,10,94,7), + dim=c(4,4), + dimnames=list(eye=c("Green","Hazel", "Blue", "Brown"), + hair=c("Black", "Brown", "Red", "Blond"))) > hec.data hair eye Black Brown Red Blond Green 5 29 14 16 Hazel 15 54 14 10 Blue 20 84 17 94 Brown 68 119 26 7 > but I want the result like below.: hair eye counts Black Green 5 Black Hazel 15 Black Blue 20 Black Brown 68 Brown Green 29 Brown Hazel 54 Brown Blue 84 Brown Brown 119 Red Green 14 Red Hazel 14 Red Blue 17 Red Brown 26 Blond Green 16 Blond Hazel 10 Blond Blue 94 Blond Brown 7 How can I do it. Thanks you for your help. Best regards, Muhammad Subianto
Using stack() would be a possibility:> a <- stack(as.data.frame(hec.data)) > a$eye=rownames(hec.data) > avalues ind eye 1 5 Black Green 2 15 Black Hazel 3 20 Black Blue 4 68 Black Brown 5 29 Brown Green 6 54 Brown Hazel 7 84 Brown Blue 8 119 Brown Brown 9 14 Red Green 10 14 Red Hazel 11 17 Red Blue 12 26 Red Brown 13 16 Blond Green 14 10 Blond Hazel 15 94 Blond Blue 16 7 Blond Brown Also have a look at reshape(). cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/~pagel
as.vector is a possible, simple solution Also use rep() on dimnames(hec.data)[[1]] to get the names vector with correct length> a<-matrix(1:15,ncol=5) > a[,1] [,2] [,3] [,4] [,5] [1,] 1 4 7 10 13 [2,] 2 5 8 11 14 [3,] 3 6 9 12 15> as.vector(a)[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15> as.vector(t(a))[1] 1 4 7 10 13 2 5 8 11 14 3 6 9 12 15 hope this helps, vito ----- Original Message ----- From: Muhammad Subianto <subianto at cs.uu.nl> To: <r-help at stat.math.ethz.ch> Sent: Friday, February 27, 2004 12:28 PM Subject: [R] Change the result data> Dear R-helper, > > I have a data like: > > > hec.data <-array(c(5,15,20,68,29,54,84,119,14,14,17,26,16,10,94,7), > + dim=c(4,4), > + dimnames=list(eye=c("Green","Hazel", "Blue", "Brown"), > + hair=c("Black", "Brown", "Red", "Blond"))) > > hec.data > hair > eye Black Brown Red Blond > Green 5 29 14 16 > Hazel 15 54 14 10 > Blue 20 84 17 94 > Brown 68 119 26 7 > > > > but I want the result like below.: > > hair eye counts > Black Green 5 > Black Hazel 15 > Black Blue 20 > Black Brown 68 > Brown Green 29 > Brown Hazel 54 > Brown Blue 84 > Brown Brown 119 > Red Green 14 > Red Hazel 14 > Red Blue 17 > Red Brown 26 > Blond Green 16 > Blond Hazel 10 > Blond Blue 94 > Blond Brown 7 > > How can I do it. Thanks you for your help. > > Best regards, > Muhammad Subianto > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
Dear Muhammad, One approach is: class(hec.data) <- "table" as.data.frame(hec.data) I hope that this helps, John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Muhammad Subianto > Sent: Friday, February 27, 2004 6:28 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Change the result data > > Dear R-helper, > > I have a data like: > > > hec.data <-array(c(5,15,20,68,29,54,84,119,14,14,17,26,16,10,94,7), > + dim=c(4,4), > + dimnames=list(eye=c("Green","Hazel", "Blue", "Brown"), > + hair=c("Black", "Brown", "Red", "Blond"))) > > hec.data > hair > eye Black Brown Red Blond > Green 5 29 14 16 > Hazel 15 54 14 10 > Blue 20 84 17 94 > Brown 68 119 26 7 > > > > but I want the result like below.: > > hair eye counts > Black Green 5 > Black Hazel 15 > Black Blue 20 > Black Brown 68 > Brown Green 29 > Brown Hazel 54 > Brown Blue 84 > Brown Brown 119 > Red Green 14 > Red Hazel 14 > Red Blue 17 > Red Brown 26 > Blond Green 16 > Blond Hazel 10 > Blond Blue 94 > Blond Brown 7 > > How can I do it. Thanks you for your help. > > Best regards, > Muhammad Subianto
Dear R-helper, I use like this below (from Prof. Peter Dalgaard) and thanks to other R-helper for your help. Best regard, Muhammad Subianto>>as.data.frame(as.table(hec.data)) >> >> > eye hair Freq >1 Green Black 5 >2 Hazel Black 15 >3 Blue Black 20 >.... > > >