[R] ordering levels
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Wed Dec 22 13:37:30 CET 2004
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 ------------------------------
More information about the R-help
mailing list