4

It would be great if somebody could explain what is going on.

 (ii <- order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1), z <- c(2,1:9)))
 ## 6  5  2  1  7  4 10  8  3  9

Yes, I did read the manual, that's from where I got the example in the first place.

What is ii?

EDIT: Considering a simpler example:

x <- c(1,1,3:1,1:4,3)
[1] 1 1 3 2 1 1 2 3 4 3                (&)
order(x)
[1]  1  2  5  6  4  7  3  8 10  9     (&&)

All I'm getting here (I believe) is that the '10' in (&&) corresponds to the '4' in (&) and means that the '4' has sort of rank (or "level") 10. Right? Put differently, the '4' is the 10th element in the ordered x.

TMOTTM
  • 3,286
  • 6
  • 32
  • 63
  • `ii` is simply a variable where the output of the `order()` function has been stored. – gung - Reinstate Monica Nov 17 '13 at 00:30
  • well, I do get that... but I don't really get what the output *means* – TMOTTM Nov 17 '13 at 14:23
  • Did you read the linked thread? Do you still not get what it means after reading that & @SimonO101's answer below? Let me know if you're still confused after reading them & what exactly is still confusing you. – gung - Reinstate Monica Nov 17 '13 at 15:02
  • Yes, I get it from your post there: "?order tells you which element of the original vector needs to be put first, second, etc.," That explains it, thanks. – TMOTTM Nov 17 '13 at 15:30
  • If you wanted to sort the vector `c(45, 50, 10, 96)` into ascending order (ie, `10 45 50 96`), which element would you put 1st? It's the 3rd element, `10`; the element you would put 2nd is the 1st number in the vector, `45`; the element you would put 3rd is the 2nd number in the vector, `50`; & the element you would put 4th is the 4th number in the vector, `96`. So the output of `order()` for that vector is `3 1 2 4`. – gung - Reinstate Monica Nov 17 '13 at 15:53
  • Those values are indexes of the elements in the original vector, ie `a[3]` is `10`. This example is easy enough to see, so you could do it manually (`a[c(3,1,2,4)]`), but since `order()` automatically outputs `3 1 2 4`, it's convenient to do `a[order(a)]` instead. Does it make sense now? – gung - Reinstate Monica Nov 17 '13 at 15:54
  • Oh, maybe I misread your comment. I think you're saying that you do understand it. I thought you were saying that was the part you didn't understand; feel free to ignore my above comments, if they're unnecessary. – gung - Reinstate Monica Nov 17 '13 at 16:00

1 Answers1

4

It returns the order of each element in x, (not as some think the elements of x ordered). When there are two identical elements in the first argument to order (x in your example above ) ties are broken by further arguments to order (y and z in your order example). This visual example, showing the order returned will hopefully explain it in fewer words...

cbind(x[ii],y[ii],z[ii])
#      [,1] [,2] [,3]
# [1,]    1    5    5   |===> 4 values of 1 in x, first value is selected by lowest value (5) in y
# [2,]    1    6    4   |
# [3,]    1    9    1   |:==> both have 9 in y, tie is broken by 1 in z
# [4,]    1    9    2   |:==> value of 2 in z
# [5,]    2    4    6
# [6,]    2    7    3
# [7,]    3    1    9
# [8,]    3    3    7
# [9,]    3    8    2
#[10,]    4    2    8
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • In this example: `> x <- c(9, 6, 7, 8)` I would expect that `order(x) gives me back `4, 1, 2 3` but it returns `2, 3, 4, 1`. How can I predict the result of `order` here in this simple example? – TMOTTM Nov 17 '13 at 14:57
  • 1
    @TMOTTM Sure. In this example the positions of the numbers in the `x` vector are: 9 = 1 , 6 = 2 , 7 = 3 , 8 = 4, now order the actual numbers, you get 6=2 , 7=3 , 8=4 , 9=1, hence the 2,3,4,1. Does that make sense?!!!! – Simon O'Hanlon Nov 17 '13 at 17:17
  • totally does. appreciate it. – TMOTTM Nov 17 '13 at 18:10