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

From: Charles C. Berry <cberry_at_tajo.ucsd.edu>
Date: Wed, 18 Jun 2008 10:32:16 -0700

On Wed, 18 Jun 2008, Andrew Yee wrote:

> Thanks for everyone's suggestions.
>
> I think factor(foo, levels = unique(foo)) works best for my needs.
>
> (By the way, I'm still trying to figure out how to use the ordered
> option in factor().)

Some functions behave differently if the factor levels are in a meaningful order, and to let them know that use ordered = TRUE

Observe the difference:

``` 	y <- rnorm(5)
lm( y ~ factor( letters[1:5] , ordered=FALSE ))
lm( y ~ factor( letters[1:5] , ordered=TRUE ))

```

HTH, Chuck

>
> Andrew
>
> On Wed, Jun 18, 2008 at 12:32 AM, Peter Alspach
> <PAlspach_at_hortresearch.co.nz> wrote:
>> 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
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.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.
>>
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

```Charles C. Berry                            (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry_at_tajo.ucsd.edu	            UC San Diego
```
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901

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 - 17:35:38 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 - 18:30:50 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.