[R] I don't understand the 'order' function
Sarah Goslee
sarah.goslee at gmail.com
Tue Apr 16 19:57:27 CEST 2013
Hi Julio,
On Tue, Apr 16, 2013 at 1:51 PM, Julio Sergio <juliosergio at gmail.com> wrote:
> I thought I've understood the 'order' function, using simple examples like:
>
> order(c(5,4,-2))
> [1] 3 2 1
>
> However, I arrived to the following example:
>
> order(c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045))
> [1] 8 9 10 7 11 6 5 4 3 2 1
>
> and I was completely perplexed!
> Shouldn't the output vector be 11 10 9 8 7 6 4 1 2 3 5 ?
> Do I have a damaged version of R?
Your version of R is fine; your understanding is damaged. :)
order() returns the element indices for each position. So in your
example, the sorted version of the vector would have element 8 in the
first place, element 9 in the second place, and element 1 in the last
place. order() is not the same as rank().
See:
x <- c(2465, 2255, 2085, 1545, 1335, 1210, 920, 210, 210, 505, 1045)
order(x)
x[order(x)]
rank(x) # what you seem to expect
Sarah
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list