Hi guys I've got an easy question but couldn't find any quick solution. I woulk like to change the following matrix good bad worse Blue 1 2 2 Yellow 2 1 3 Black 3 4 4 Into the following structure good 1 Blue Bad 2 Blue Worse 2 Blue Good 2 Yellow Bad 1 Yellow Worse 2 Yellow Good 2 Black Bad 4 Black Worse 4 Black Thanks a lot. Regards Beat Huggler --- Beat Huggler Quantitative Analysis RMF Investment Products Huobstrasse 16 8808 Pfaeffikon SZ Switzerland www.rmf.ch Tel. +41 55 415 87 30 Fax +41 55 415 87 07 This message and any attachment are confidential and may be privileged or otherwise protected from disclosure. If you are not the intended recipient, please telephone or email the sender and delete this message and any attachment from your system. If you are not the intended recipient you must not copy this message or attachment or disclose the contents to any other person. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 15 May 2002, Beat Huggler wrote:> > Hi guys>> I've got an easy question but couldn't find any quick solution. I woulk > like to change the following matrix > > good bad worse > Blue 1 2 2 > Yellow 2 1 3 > Black 3 4 4 > > Into the following structure > > good 1 Blue > Bad 2 Blue > Worse 2 Blue > Good 2 Yellow > Bad 1 Yellow > Worse 2 Yellow > Good 2 Black > Bad 4 Black > Worse 4 Black >reshape() will do this, but you need a dataframe and need the `colors' to be a column of the data frame, so if it is really a matrix df<-as.data.frame(X) df$colors<-colnames(X) A simple version is reshape(df,direction=long, varying=list(c("good","bad","worse"))) color time good id 1.1 Blue 1 1 1 2.1 Yellow 1 2 2 3.1 Black 1 2 3 1.2 Blue 2 2 1 2.2 Yellow 2 1 2 3.2 Black 2 4 3 1.3 Blue 3 2 1 2.3 Yellow 3 3 2 3.3 Black 3 4 3 and more precisely what you want is reshape(df,direction="long",varying=list(c("good","bad","worse")),times=c("good","bad","worse"),idvar="color") color time good Blue.good Blue good 1 Yellow.good Yellow good 2 Black.good Black good 2 Blue.bad Blue bad 2 Yellow.bad Yellow bad 1 Black.bad Black bad 4 Blue.worse Blue worse 2 Yellow.worse Yellow worse 3 Black.worse Black worse 4 As you can see, reshape() is overdesigned for a problem like this, but I assume your problem is in fact more complicated. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Beat Huggler wrote:> I've got an easy question but couldn't find any quick > solution. I woulk like to change the following matrix > > good bad worse > Blue 1 2 2 > Yellow 2 1 3 > Black 3 4 4 > > Into the following structure > > good 1 Blue > Bad 2 Blue > Worse 2 Blue > Good 2 Yellow > Bad 1 Yellow > Worse 2 Yellow > Good 2 Black > Bad 4 Black > Worse 4 BlackTry this: R> dat good bad worse blue 1 2 2 yellow 2 1 3 black 3 4 4 R> R> print (cbind ( + rep (colnames (dat), 3), + as.vector (t (dat)), + rep (rownames (dat), 1, each=3)), + quote=FALSE) [,1] [,2] [,3] [1,] good 1 blue [2,] bad 2 blue [3,] worse 2 blue [4,] good 2 yellow [5,] bad 1 yellow [6,] worse 3 yellow [7,] good 3 black [8,] bad 4 black [9,] worse 4 black -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Beat Huggler wrote:> > Hi guys > > I've got an easy question but couldn't find any quick solution. I woulk > like to change the following matrix > > good bad worse > Blue 1 2 2 > Yellow 2 1 3 > Black 3 4 4 > > Into the following structure > > good 1 Blue > Bad 2 Blue > Worse 2 Blue > Good 2 Yellow > Bad 1 Yellow > Worse 2 Yellow > Good 2 Black > Bad 4 Black > Worse 4 BlackIf your problem is that simple, you can do the following: starting from a matrix R> my.matrix good bad worse Blue 1 2 2 Yellow 2 1 3 Black 3 4 4 You can simply say: R> as.data.frame(as.table(my.matrix)) Var1 Var2 Freq 1 Blue good 1 2 Yellow good 2 3 Black good 3 4 Blue bad 2 5 Yellow bad 1 6 Black bad 4 7 Blue worse 2 8 Yellow worse 3 9 Black worse 4 If your problem is more complex, then use reshape() as Thomas suggested. Best, Z> Thanks a lot. > > Regards > > Beat Huggler > > --- > Beat Huggler > Quantitative Analysis > RMF Investment Products > Huobstrasse 16 > 8808 Pfaeffikon SZ > Switzerland > www.rmf.ch > Tel. +41 55 415 87 30 > Fax +41 55 415 87 07 > > This message and any attachment are confidential and may be privileged > or otherwise protected from disclosure. If you are not the intended > recipient, please telephone or email the sender and delete this message > and any attachment from your system. If you are not the intended > recipient you must not copy this message or attachment or disclose the > contents to any other person. > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._