# [R] constructing specially ordered factor

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Mon Oct 4 12:54:16 CEST 2004

```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

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)
>
> Valery.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help