Hi all, my colleagues deal with tables, where every factor is represented in two columns. The first column contains some numeric codes and the second contains the corresponding symbolic name. For example: ISEX SSEX 0 Female 1 Male 0 Female 0 Female ... another example: ICONC SCONC 10 Normal 1000 ExtraHigh 10 Normal 0 Nothing 100 High ... Colleagues require that the ordering should be done always by numeric column and not by the column with symbolic equivalents. Here comes the question: Is it possible to create factor with properly ordered and labeled values in nicer form then in the following long solution: Factor<-function(Names,Weights) { iunique = !duplicated(Weights) uniqueWeights = Weights[iunique] uniqueNames = Names[iunique] # corresponding unique names factor(Names, uniqueNames[order(uniqueWeights)]) } Factor(SSEX, ISEX) Factor(SCONC, ICONC) Thank you in advance for the comments, Valery.
Hi Valery, does the following work in your data: levs <- unique.default(Names) factor(Names, levs[order(unique.default(Weights))]) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Khamenia, Valery" <V.Khamenia at biovision-discovery.de> To: <R-help at stat.math.ethz.ch> Sent: Monday, October 04, 2004 12:01 PM Subject: [R] constructing specially ordered factor> Hi all, > > my colleagues deal with tables, where every factor is > represented in two columns. The first column contains some > numeric codes and the second contains the corresponding > symbolic name. For example: > > ISEX SSEX > 0 Female > 1 Male > 0 Female > 0 Female > ... > > another example: > > ICONC SCONC > 10 Normal > 1000 ExtraHigh > 10 Normal > 0 Nothing > 100 High > ... > > Colleagues require that the ordering should be done > always by numeric column and not by the column with > symbolic equivalents. > > Here comes the question: > > Is it possible to create factor with properly ordered and > labeled values in nicer form then in the following long > solution: > > Factor<-function(Names,Weights) { > iunique = !duplicated(Weights) > uniqueWeights = Weights[iunique] > uniqueNames = Names[iunique] # corresponding unique names > factor(Names, uniqueNames[order(uniqueWeights)]) > } > > Factor(SSEX, ISEX) > > Factor(SCONC, ICONC) > > Thank you in advance for the comments, > Valery. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On 4 Oct 2004 at 12:01, Khamenia, Valery wrote:> Hi all, > > my colleagues deal with tables, where every factor is > represented in two columns. The first column contains some > numeric codes and the second contains the corresponding > symbolic name. For example: > > ISEX SSEX > 0 Female > 1 Male > 0 Female > 0 Female > ... > > another example: > > ICONC SCONC > 10 Normal > 1000 ExtraHigh > 10 Normal > 0 Nothing > 100 High > ... > > Colleagues require that the ordering should be done > always by numeric column and not by the column with > symbolic equivalents. > > Here comes the question: > > Is it possible to create factor with properly ordered and > labeled values in nicer form then in the following long > solution: > > Factor<-function(Names,Weights) { > iunique = !duplicated(Weights) > uniqueWeights = Weights[iunique] > uniqueNames = Names[iunique] # corresponding unique names > factor(Names, uniqueNames[order(uniqueWeights)]) > } > > Factor(SSEX, ISEX) > > Factor(SCONC, ICONC)Hallo Is that what you want?> ooo<-order(levels(factor(pokus$ICONC)), decreasing=T) > my.order<-levels(factor(pokus$SCONC))[ooo] > factor(pokus$SCONC, levels=my.order)[1] Normal ExtraHigh Normal Nothing High Levels: Nothing Normal High ExtraHigh If you want it in a function Factor <- function(f,n, decreasing=TRUE, ...) { ooo<-order(levels(factor(n)), decreasing=decreasing) my.order<-levels(factor(f))[ooo] factor(f, levels=my.order) } Cheers Petr> > Thank you in advance for the comments, > Valery. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz