1

Why the order function in R, yields an ascending output on same values within the vector?

abc=c("100","1","100")
order(abc)
[1] 2 1 3

Why 2 1 3?? Instead of 2 1 2

  • 2
    how can 2 numbers be in one position?? That is imposiible.. whether it is 3,1,2 or 2,1,3 any does not matter as since both position 2 and 3 wil be occupied by "100" but they can not both be 2 since 2 numbers cannot occupy 1 position – Onyambu Aug 22 '18 at 18:23
  • You can use `data.table::frank(abc, ties.method = "dense")` -- though "order" and "rank" are used for different things, so this might not fit how you want to use the result... – Frank Aug 22 '18 at 18:28
  • 1
    `order` is usually used for putting things in order by subsetting, e.g. `abc[order(abc)]`, which returns the same thing as `sort(abc)`. As Frank mentioned, it sounds like you may be looking for `rank` or its variants, in which case its `ties.method` parameter will determine the behavior you're referring to. – alistaire Aug 22 '18 at 18:39
  • 2
    I think there's several issues here - (a) probably you want to be using numbers `c(100, 1, 100)` rather than strings (no quotes in the definition)- assuming you want `2` to show up before `100`, not after. (b) You may be confusing `order` and `rank` - I'd suggest reading the FAQ [Understanding the `order` function](https://stackoverflow.com/q/2315601/903061). (c) `rank` has several options for how to treat ties. – Gregor Thomas Aug 22 '18 at 18:40
  • 2
    Also, be careful—strings of numbers (quoted with `""`) are not the same as numbers, so `sort`, `order`, and `rank` will sort character-by-character ("alphabetically", broadly defined) instead of by number size, so `"100" < "2"`. Convert with `as.integer` or `as.numeric`, or fix the type upstream. – alistaire Aug 22 '18 at 18:43

0 Answers0