Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • order() get random result

    group = (1 \ 1 \ 1 \ 2 \ 2 \ 2 \ 3 \ 3 \ 4 \ 5 \ 5 \ 6 \ 7 \ 8 \ 8 \ 8)
    order(group,1)

    The result seems to be ramdon.


    : order(group,1)
    1
    +------+
    1 | 2 |
    2 | 1 |
    3 | 3 |
    4 | 5 |
    5 | 4 |
    6 | 6 |
    7 | 8 |
    8 | 7 |
    9 | 9 |
    10 | 11 |
    11 | 10 |
    12 | 12 |
    13 | 13 |
    14 | 14 |
    15 | 16 |
    16 | 15 |
    +------+

    : order(group,1)
    1
    +------+
    1 | 2 |
    2 | 3 |
    3 | 1 |
    4 | 6 |
    5 | 4 |
    6 | 5 |
    7 | 7 |
    8 | 8 |
    9 | 9 |
    10 | 10 |
    11 | 11 |
    12 | 12 |
    13 | 13 |
    14 | 14 |
    15 | 16 |
    16 | 15 |
    +------+


    I expect that the result is

    1
    +------+
    1 | 1 |
    2 | 2 |
    3 | 3 |
    4 | 4 |
    5 | 5 |
    6 | 6 |
    7 | 7 |
    8 | 8 |
    9 | 9 |
    10 | 10 |
    11 | 11 |
    12 | 12 |
    13 | 13 |
    14 | 14 |
    15 | 15 |
    16 | 16 |
    +------+

    Is there any way to do this.

    Thanks!

  • #2
    Why would you expect the first value 1 to be less than the second value 1? There is no way to say so, so Stata does the only sensible thing and sorts them in a random order. You can make an additional vector storing the original sort order and use that as a secondary variable on which to sort your data. However, I strongly recommend you reconsider this. Such sorts depending on the original sort order are extremely fragile. Instead I would look into getting a unique sort by substantive means.


    Code:
    mata
    group = (1 \ 1 \ 1 \ 2 \ 2 \ 2 \ 3 \ 3 \ 4 \ 5 \ 5 \ 6 \ 7 \ 8 \ 8 \ 8)
    orig_order = 1::16
    order((group, orig_order),(1,2))
    end
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks, but the random order really surprised me.

      Comment

      Working...
      X