The order and rank functions do something of an inverse of each other. The rank function tells you the rank of each element of the vector (if the first element is the 2nd smallest, then the first element of the return in 2). The order function tells you what order to put the vector in to sort it (if the smallest element of the vector is in position 3, then the first element of the returned vector will be 3). Doing something like:
> order(order(x)) Is similar to rank, except in how it deals with ties. When the data is presorted, then rank and order both give you the same as seq(along=x) which is just the set of integers from 1 to the length of the vector. Most computations in R will switch between integer and double automatically, but if you really need a vector to be integer, then use the as.integer function. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 > -----Original Message----- > From: Ken Williams [mailto:[EMAIL PROTECTED] > Sent: Thursday, July 12, 2007 3:50 PM > To: Greg Snow; [email protected] > Subject: Re: [R] Compute rank within factor groups > > > > > On 7/12/07 4:28 PM, "Greg Snow" > <[EMAIL PROTECTED]> wrote: > > > Why are you using order instead of rank? > > > > If the data is pre sorted then they tend to give the same result > > (unless there are ties), but if your data is not presorted, > then the > > results will be different. > > Indeed, thanks for the catch. I switched to order because > rank was giving me floats instead of integers, which I now > see was probably because it defaults to ties.method=average > and I wanted ties.method=first. > > My data was indeed pre-sorted so I didn't notice the > difference (are they giving inverse permutations or > something? Can't quite follow...), but perhaps it won't always be. > > > -- > Ken Williams > Research Scientist > The Thomson Corporation > Eagan, MN > > ______________________________________________ [email protected] 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.
