Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: RE: st: assignment by indexing


From   [email protected] (William Gould, Stata)
To   [email protected]
Subject   Re: RE: st: assignment by indexing
Date   Tue, 03 Feb 2004 09:03:35 -0600

Allan Reese <[email protected]> correctly noted that 

        . gen y[rankx] = x

is disallowed.  Still, there is a way to achieve the desired result:

	. gen i = _n
	. sort rankx
	. gen y = x[i]
	. sort i

Let's try this solution with a little bit of the auto data, creating 
new variable -new- equal to mpg, in order.  We'll start with, 

	. sysuse auto
	. keep make mpg
	. keep in 1/10
	. list

             +---------------------+
             | make            mpg |
             |---------------------|
          1. | AMC Concord      22 |
          2. | AMC Pacer        17 |
          3. | AMC Spirit       22 |
          4. | Buick Century    20 |
          5. | Buick Electra    15 |
             |---------------------|
          6. | Buick LeSabre    18 |
          7. | Buick Opel       26 |
          8. | Buick Regal      20 |
          9. | Buick Riviera    16 |
         10. | Buick Skylark    19 |
             +---------------------+

	. gen i = _n
	. sort mpg 
	. gen new = mpg[i]
	. sort i 
	. list make mpg new

             +---------------------------+
             | make            mpg   new |
             |---------------------------|
          1. | AMC Concord      22    15 |
          2. | AMC Pacer        17    16 |
          3. | AMC Spirit       22    17 |
          4. | Buick Century    20    18 |
          5. | Buick Electra    15    19 |
             |---------------------------|
          6. | Buick LeSabre    18    20 |
          7. | Buick Opel       26    20 |
          8. | Buick Regal      20    22 |
          9. | Buick Riviera    16    22 |
         10. | Buick Skylark    19    26 |
             +---------------------------+

How the trick works
-------------------

We create an indexing variable i = (1,2,3,...,_N) and then put the 
data in the order of mpg (or the indexing variable, or whatever).  
The reuslt of that is to permute i.  The first value of i is now where 
that that observation used to reside.  Let's look:

	. * picking up from where we left off, 
	. sort mpg                  // put the data back in mpg order
	. list

             +--------------------------------+
             | make            mpg    i   new |
             |--------------------------------|
          1. | Buick Electra    15    5    19 |
          2. | Buick Riviera    16    9    22 |
          3. | AMC Pacer        17    2    16 |
          4. | Buick LeSabre    18    6    20 |
          5. | Buick Skylark    19   10    26 |
             |--------------------------------|
          6. | Buick Century    20    4    18 |
          7. | Buick Regal      20    8    22 |
          8. | AMC Concord      22    1    15 |
          9. | AMC Spirit       22    3    17 |
         10. | Buick Opel       26    7    20 |
             +--------------------------------+

The Buick Electra was observation 5 in the original ordering.  When we 
put the data back in the original order, we want -new- to take on the 
5th value of mpg, which is to say, mpg[5].  The Buick Riviera used to be 
the 9th observation.  When we put the data back in order, we want -new- 
to take on the 9th value of mpg, which is to say, mpg[9].  Hence, 

	. gen new = mpg[i]

produces what we want and then it is just a matter of putting the data 
back into the original order.

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index