Announcement

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

  • row sorting?

    Suppose that I have the following data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id x1 x2 x3 x4 x5 x6)
    1 13 14 12  2 23 56
    2  2 34 56 43 21 12
    3  2  3 45  1 23 34
    4  4  6 13 14 22 45
    5  2  4 23 56 78 23
    end
    For each id, I want to generate y1-y6, which is corresponding to the smallest to largest values of x1-x6. Any suggestion?
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Nick Cox wrote the -rowsort- command that does this. I believe you can get it from SSC.

    Comment


    • #3
      Another approach

      Code:
      tempfile original
      save `original'
      reshape long x, i(id)
      rename x y
      sort id y
      by id: replace _j=_n
      reshape wide y, i(id)
      merge 1:1 id using `original'
      drop _merge
      list

      Code:
      . list
      
           +----------------------------------------------------------------+
           | id   y1   y2   y3   y4   y5   y6   x1   x2   x3   x4   x5   x6 |
           |----------------------------------------------------------------|
        1. |  1    2   12   13   14   23   56   13   14   12    2   23   56 |
        2. |  2    2   12   21   34   43   56    2   34   56   43   21   12 |
        3. |  3    1    2    3   23   34   45    2    3   45    1   23   34 |
        4. |  4    4    6   13   14   22   45    4    6   13   14   22   45 |
        5. |  5    2    4   23   23   56   78    2    4   23   56   78   23 |
           +----------------------------------------------------------------+
      Last edited by Andrew Musau; 11 Apr 2017, 20:59.

      Comment


      • #4
        Following Clyde's comment note that discussion of rowsort is to be found at http://www.stata-journal.com/article...article=pr0046 so that users interested should

        Code:
        search rowsort
        in Stata and download from the Stata Journal site thus indicated. The URL above leads to a freely accessible pdf of a 2009 paper. Stata 9 is required to run that version of the code.

        The version of rowsort on SSC is an earlier and less versatile version for Stata 8. Even now some people use that version for good reason, so the original motive of letting the earlier version remain accessible still holds sound.

        To keep track of what I have made public where, I started keeping details in a help file.

        Code:
        ssc inst njc_stuff
        installs the public version which in this case documents

        Code:
            rowsort
            SSC (NJC)
            row sort a set of integer variables
            sup(SJ 9-1)
        
            rowsort 
            pr0046 sj9-1 (NJC) 
            row sort a set of variables
            +
        with annotations there explained.

        All that said, the best single piece of advice to people asking for this is to consider

        Code:
        reshape long 
        as the resulting data structure is in most contexts easier to work with.

        Comment


        • #5
          Clyde, @Andrew, and @Nick, thank you all for the help.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Since an observation's values are sorted independently from the other observations, here's an out of left field solution: use rangestat to perform the task. You define an interval that will pick-up only the current observation by using variable id. Then you need to define a simple Mata function that takes the rowvector X and sorts it. Mata only sorts by rows so the function transposes back and forth.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(id x1 x2 x3 x4 x5 x6)
            1 13 14 12  2 23 56
            2  2 34 56 43 21 12
            3  2  3 45  1 23 34
            4  4  6 13 14 22 45
            5  2  4 23 56 78 23
            end
            
            * define a Mata function that sorts a rowvector
            mata:  
                mata clear
                real rowvector myrowsort(real rowvector X) {
                    return(sort(X',1)')
                }
            end 
            
            rangestat (myrowsort) x1 x2 x3 x4 x5 x6, interval(id 0 0)
            Code:
            . list, noobs compress
            
              +----------------------------------------------------------------------------------+
              | id   x1   x2   x3   x4   x5   x6   myr~1   myr~2   myr~3   myr~4   myr~5   myr~6 |
              |----------------------------------------------------------------------------------|
              |  1   13   14   12    2   23   56       2      12      13      14      23      56 |
              |  2    2   34   56   43   21   12       2      12      21      34      43      56 |
              |  3    2    3   45    1   23   34       1       2       3      23      34      45 |
              |  4    4    6   13   14   22   45       4       6      13      14      22      45 |
              |  5    2    4   23   56   78   23       2       4      23      23      56      78 |
              +----------------------------------------------------------------------------------+

            Comment


            • #7
              Robert: Impressive. There are so many handles in rangestat to add things to! That one should go in the help.

              For the benefit of new readers, rangestat is from SSC and written by Robert Picard and a couple of other guys. You need to install it before you can use it, with

              Code:
              ssc inst rangestat

              Comment


              • #8
                Nick is too modest: he is one of the "other guys" who wrote -rangestat-. The other one is Roberto Ferrer.

                Comment


                • #9
                  #7 was an example of second-order immodesty!

                  Comment

                  Working...
                  X