[R] Sorting problem

Duncan Murdoch murdoch at stats.uwo.ca
Sun Mar 29 12:15:58 CEST 2009


On 28/03/2009 4:57 PM, Stavros Macrakis wrote:
> On Sat, Mar 28, 2009 at 7:53 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> ...More generally, the xtfrm() function converts a vector into a numeric one that sorts in the same order. ...
> 
> Thanks, I learn a lot just by reading the answers to other people's
> questions on this list.
> 
> Some followup questions:
> 
> 1) Where does the name 'xtfrm' come from?

I don't know.

> 2) Why isn't xtfrm of a numeric vector the identity function?

"Because the implementation does that".  It's a one liner:

xtfrm.default <- function (x)
  as.vector(rank(x, ties.method = "min", na.last = "keep"))

The identity function would probably be faster, but why optimize cases 
that are never needed?  A better optimization would be just to use 
order(x), not order(xtfrm(x)).

Duncan Murdoch

> 3) Similarly, why isn't xtfrm of a logical vector just as.integer of
> it? (as it is for factors)

> 4) Why is xtfrm(c(2,3,2)) => c(1,3,1) and not c(1,2,1) or for that
> matter c(2,3,2) (see point 2)?




More information about the R-help mailing list