0
xx = c("calculated_p3", "calculated_c1" ,"calculated_p2" ,"calculated_c2", "calculated_d2",
"calculated_d3", "calculated_c3", "calculated_p1" ,"calculated_d1")

order(xx)

The output is: 2 4 7 9 5 6 8 3 1

Why is the "calculated_d1" ordered as the first element? And why is "calculated_c2" ordered as the 9th element? I don't understand here. Shouldn't "calculated_c1" be the first one?

Thank you for your help

Cairo
  • 21
  • 3
  • 1
    Yes, index #2 is the first ordered element and that is the original "_c1". Do: `xx[order(xx)]` and you'll get them ordered correctly – R. Schifini Dec 19 '19 at 05:32
  • This might be helpful to understand the functions https://stackoverflow.com/questions/54017285/difference-between-sort-rank-and-order-in-r . Maybe you just need `sort(xx)` – Ronak Shah Dec 19 '19 at 05:33

2 Answers2

2

order is written such that xx[order(xx)] is the same as sort(xx).

The numbers don't refer to the position that each entry should go to but rather the position the entries should come from if they were in order.

calculated_c1 should indeed be the first one. As it is in position 2, the first number is therefore a 2.

Daniel V
  • 1,305
  • 7
  • 23
0

If you want to keep your order you can use factors:

factor(xx, xx)
[1] calculated_p3 calculated_c1 calculated_p2 calculated_c2 calculated_d2 calculated_d3 calculated_c3 calculated_p1
[9] calculated_d1
9 Levels: calculated_p3 calculated_c1 calculated_p2 calculated_c2 calculated_d2 calculated_d3 ... calculated_d1
Florian
  • 1,248
  • 7
  • 21