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
> 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 stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Andrew levels(factor(foo, levels=c('b','a'))) should work. You can make foo an ordered factor too, but that is not necessary. HTH ....... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Andrew Yee > Sent: Wednesday, 18 June 2008 4:22 p.m. > 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >The contents of this e-mail are privileged and/or confidential to the named recipient and are not to be used by any other person and/or organisation. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail.