Re: [R] keeping original order in factor()

From: Steven McKinney <smckinney_at_bccrc.ca>
Date: Tue, 17 Jun 2008 21:30:25 -0700

> foo <- sample(letters, 10, replace = TRUE)
> foo

 [1] "i" "j" "u" "f" "a" "k" "m" "k" "i" "w"
> unique(foo)

[1] "i" "j" "u" "f" "a" "k" "m" "w"
> bar <- factor(foo, levels = unique(foo))
> bar

 [1] i j u f a k m k i w
Levels: i j u f a k m w
>

Is this what you are trying to do?

HTH Steve McKinney

-----Original Message-----
From: r-help-bounces_at_r-project.org on behalf of Andrew Yee Sent: Tue 6/17/2008 9:22 PM
To: r-help_at_r-project.org
Subject: [R] keeping original order in factor()  

Apologies for the naieveness of this question, but I'm having trouble figuring out to have factor() maintain original ordering.

For example,

foo <- c("b","b","a","a")
levels(factor(foo, ordered=T)) #I'd like this to return as "b" "a"

                                          #not "a" "b"

I thought having ordered=T would do the trick.

Thanks,
Andrew



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 18 Jun 2008 - 04:56:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 18 Jun 2008 - 05:31:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive