[Rd] suggestion for ?factor

peter dalgaard pdalgd at gmail.com
Mon Aug 9 18:28:33 CEST 2010


On Aug 9, 2010, at 5:37 PM, (Ted Harding) wrote:

> Hi Folks.
> May I suggest a small addition to ?factor -- this is not explicit
> about how to force the ordering of levels in an ordered factor.
> 
> One can guess it, but I think it would be a good idea to spell it out.
> For example, one can do:
> 
>  X <- c("Alf","Bert","Chris","Dave")
>  F <- factor(X,levels=c("Dave","Alf","Chris","Bert"),ordered=TRUE)
>  F
>  # [1] Alf   Bert  Chris Dave 
>  # Levels: Dave < Alf < Chris < Bert
> 
> So, where ?factor says:
> 
>  levels: an optional vector of the values that ?x? might have taken.
>          The default is the unique set of values taken by
>          'as.character(x)', sorted into increasing order of 'x'.
>          Note that this set can be smaller than 'sort(unique(x))'.
> 
> it could include a bit extra so as to read:
> 
>  levels: an optional vector of the values that 'x' might have taken.
>          The default is the unique set of values taken by
>          'as.character(x)', sorted into increasing order of 'x'.
>          Note that this set can be smaller than 'sort(unique(x))'.
>          If 'levels=...' is present and 'ordered=TRUE', then the
>          levels are ordered according to the order in which they
>          are listed in 'levels'.

Hmm, yes, but ordered=TRUE is actually irrelevant, levels have orderings regardless (it matters for tabulation purposes, but also for modeling).  

[And please don't get me started about the ancient mess-up of ordered factor usage in the modeling code...]

I'd suggest to say something like "If 'levels' is present, then it defines the levels and their ordering directly, possibly including values not present in 'x'." I'd be tempted also to add that it is really as.character(x) that is matched against the level set, but I'm not sure it is the right place for that.


> 
> With thanks,
> Ted.
> 
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 09-Aug-10                                       Time: 16:37:50
> ------------------------------ XFMail ------------------------------
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-devel mailing list