0

I encountered some strange behavior with the order() function. I run the following comment in the R.

> order(c(38,141,23,53,78,105))

The result is :

[1]  3 1 4 5 6 2

But I expected (if it is the descending):

5 1 6 4 3 2

(if it is the ascending)

2 6 1 3 4 5

What happen in this function? What's wrong with me? I'm afraid of understanding order() function.

Thanks!

tohappyhj
  • 1
  • 1
  • `order` is correctly returning the indices of the original vector in ascending order of the values of the original vector. (The smallest number was 23, the third element of the vector, which is why the output of `order` starts with 3. Then comes 38, the first element, and so on.) Can you explain why you were expecting to see `5 1 6 4 3 2`? – A. S. K. Nov 22 '19 at 03:21
  • @A.S.K Sorry I'm confusing the default option of order(). However, if the option is set "decreasing = TRUE" and the smallest number is 23, the third element of the vector, then I expected 23 has to be 6 - the third element of my expectation "5 1 6 4 3 2". – tohappyhj Nov 22 '19 at 03:52
  • I found for understanding of the order() function similar as my question, here is the [link](https://stackoverflow.com/questions/2315601/understanding-the-order-function) – tohappyhj Nov 22 '19 at 04:22

1 Answers1

0

order gives you the indices, in the original vector, of ordered value. Your are expecting it to return the position of the unordered value.

Ordered, the first element is at position 3. This is the awnser of order. The first element of your vector is at position 5 when ordered is not the answer of order

JRR
  • 3,024
  • 2
  • 13
  • 37