AW: [R] constructing specially ordered factor

Prof Brian Ripley ripley at
Mon Oct 4 15:41:19 CEST 2004

On Mon, 4 Oct 2004, Khamenia, Valery wrote:

> Hi Dimitris,
> thank you for your reply, 
> > does the following work in your data:
> > 
> > levs <- unique.default(Names)
> > factor(Names, levs[order(unique.default(Weights))])
> your solution is really shorter, but has two issues to be 
> meant here:
> 1. "unique.default" is applied twice, what might 
>    be a bit expensive for strings.
> 2. your solution brings an implicit prerequisit on 
>   "unique.default". Indeed, "unique.default" should 
>   preserve the same order for output both working 
>   with strings and with numbers. In other words, 
>   correspondence must be kept by "unique.default".
>   If "unique.default" implementation uses (or will use) 
>   lexicographic strings sorting for acceleration
>   then this approach fails.

Please follow the posting guide and do your homework before posting, in 
this case the help page for unique.default.

     'unique' returns a vector, data frame or array like 'x' but with
     duplicate  elements removed.


     An object of the same type of 'x'. but if an element is equal to
     one with a smaller index, it is removed.

so the order is preserved, by definition.

BTW it uses hashing for `acceleration', not something as slow as sorting.

Brian D. Ripley,                  ripley at
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list