Hello!
I would like to know if there is a simple way to reorder levels of a given
factor.Let's say that the vector
testf<-factor(c("red","red","red","blue","blue","white"))
levels(testf) : blue red white
should have reordered levels such as
levels(testf) : red blue white
(this is for presentation purposes)
I guess I'm looking for a generalized version of "relevel"...
Thanks
Anne
----------------------------------------------------
Anne Piotet
Tel: +41 79 359 83 32 (mobile)
Email: anne.piotet@m-td.com
---------------------------------------------------
M-TD Modelling and Technology Development
PSE-C
CH-1015 Lausanne
Switzerland
Tel: +41 21 693 83 98
Fax: +41 21 646 41 33
--------------------------------------------------
[[alternative HTML version deleted]]
testf <- ordered(testf, levels=c("red", "blue",
"white"))
?ordered
Anne wrote:> Hello!
> I would like to know if there is a simple way to reorder levels of a given
factor.Let's say that the vector
>
testf<-factor(c("red","red","red","blue","blue","white"))
> levels(testf) : blue red white
>
> should have reordered levels such as
> levels(testf) : red blue white
>
> (this is for presentation purposes)
> I guess I'm looking for a generalized version of "relevel"...
>
> Thanks
>
> Anne
>
> ----------------------------------------------------
> Anne Piotet
> Tel: +41 79 359 83 32 (mobile)
> Email: anne.piotet at m-td.com
> ---------------------------------------------------
> M-TD Modelling and Technology Development
> PSE-C
> CH-1015 Lausanne
> Switzerland
> Tel: +41 21 693 83 98
> Fax: +41 21 646 41 33
> --------------------------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894
In: http://biostat.ku.dk/~bxc/SPE/library/ you will find a zip of the Lexis package that contains the function Relevel, which has precise this (and other) features. Bendix ---------------------- Bendix Carstensen Senior Statistician Steno Diabetes Center Niels Steensens Vej 2 DK-2820 Gentofte Denmark tel: +45 44 43 87 38 mob: +45 30 75 87 38 fax: +45 44 43 07 06 bxc at steno.dk www.biostat.ku.dk/~bxc ----------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Anne > Sent: Wednesday, December 22, 2004 1:06 PM > To: R list > Subject: [R] ordering levels > > > Hello! > I would like to know if there is a simple way to reorder > levels of a given factor.Let's say that the vector > testf<-factor(c("red","red","red","blue","blue","white")) > levels(testf) : blue red white > > should have reordered levels such as > levels(testf) : red blue white > > (this is for presentation purposes) > I guess I'm looking for a generalized version of "relevel"... > > Thanks > > Anne > > ---------------------------------------------------- > Anne Piotet > Tel: +41 79 359 83 32 (mobile) > Email: anne.piotet at m-td.com > --------------------------------------------------- > M-TD Modelling and Technology Development > PSE-C > CH-1015 Lausanne > Switzerland > Tel: +41 21 693 83 98 > Fax: +41 21 646 41 33 > -------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 Wed, 22 Dec 2004 13:06:02 +0100 Anne wrote:> Hello! > I would like to know if there is a simple way to reorder levels of a > given factor.Let's say that the vector > testf<-factor(c("red","red","red","blue","blue","white")) > levels(testf) : blue red white > > should have reordered levels such as > levels(testf) : red blue whiteYou can do testf <- factor(as.character(testf), levels = c("red", "blue", "white")) or simply create testf with the right ordering of levels in the first place. hth, Z> (this is for presentation purposes) > I guess I'm looking for a generalized version of "relevel"... > > Thanks > > Anne > > ---------------------------------------------------- > Anne Piotet > Tel: +41 79 359 83 32 (mobile) > Email: anne.piotet at m-td.com > --------------------------------------------------- > M-TD Modelling and Technology Development > PSE-C > CH-1015 Lausanne > Switzerland > Tel: +41 21 693 83 98 > Fax: +41 21 646 41 33 > -------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 22-Dec-04 Anne wrote:> Hello! > I would like to know if there is a simple way to reorder levels > of a given factor.Let's say that the vector > testf<-factor(c("red","red","red","blue","blue","white")) > levels(testf) : blue red white > > should have reordered levels such as > levels(testf) : red blue white > > (this is for presentation purposes) > I guess I'm looking for a generalized version of "relevel"...Hi Anne, Look at ?factor and note the default levels = sort(unique.default(x), na.last = TRUE) in factor(x, levels = sort(unique.default(x), na.last = TRUE), labels = levels, exclude = NA, ordered = is.ordered(x)) You can change the order of levels by changing the "levels = ...", as in> factor(c("1.2nd","2.1st","3.4th","4.3rd"))[1] 1.2nd 2.1st 3.4th 4.3rd Levels: 1.2nd 2.1st 3.4th 4.3rd> factor(c("1.2nd","2.1st","3.4th","4.3rd"),levels=c("2.1st","1.2nd","4.3rd","3.4th")) [1] 1.2nd 2.1st 3.4th 4.3rd Levels: 2.1st 1.2nd 4.3rd 3.4th Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 [NB: New number!] Date: 22-Dec-04 Time: 12:37:30 ------------------------------ XFMail ------------------------------