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

st: RE: reshaping data


From   "Wallace, John" <John_Wallace@affymetrix.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: reshaping data
Date   Fri, 24 Jun 2005 12:54:17 -0700

You first need to identify which variable(s) uniquely identifies the
observations you want to reshape.  In this case, its your Y variable, or
"comp" in the bottom table.  In Stata's reshape syntax, this is the "i"
parameter
.reshape wide [variable], i([variables]) j([variable]) 

You next need to determine which variable defines the new columns - in
Stata's reshape syntax, this is the "j" parameter.  In this case it is
your X variable, or in the lower dataset, the "gstn" variable.

This leaves Z (or "distance") as the data which is being reshaped.
Plugging it all together gives
.reshape wide Z, i(Y) j(X)

or

.reshape wide distance, i(comp) j(gstn)

had gstn been a string variable instead of a number, you would specify
the string option, thus:

.reshape wide distance, i(comp) j(gstn) string

Reshape is a notoriously tricky idea to wrap your head around, and there
are extensive FAQ examples that deal with reshaping wide to long and
vice versa.

Incidentally, if once you successfully reshape your data into wide
format you want to return it to long, all you need to do is type -
reshape long -.

Good luck
-JW

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Mazhar Islam
Sent: Friday, June 24, 2005 12:35 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: reshaping data

I have a dataset with the following format:

X    Y     Z
1   12    2.5 
1    3    3.4
1    7    8.9
2   18    0.9
2    6    1.9
2   11    3.7
3   5     1.2
3   19    4.5
3    8    5.6

I want to reshape it as follows:

X   Y1   Z1   Y2   Z2    Y3   Z3  
1   12   2.5   3   3.4    7   8.9
2   18   0.9   6   1.9   11   3.7
3    5   1.2  19   4.5    8   5.6

I know I need to use reshape command. But don't know
how. Can anyone please help? Thanks a lot.
Mazhar








+-------------------------+
      | gstn   comp    distance |
      |-------------------------|
   1. |    4    105   7174.6529 |
   2. |    4     52   9432.1751 |
   3. |    4     55   9760.1456 |
   4. |    4     59   10351.248 |
   5. |    4     57   10590.998 |
      |-------------------------|
   6. |    5    691   8421.3531 |
   7. |    5    111   8912.8855 |
   8. |    5    109   9413.6016 |
   9. |    5      7   10006.499 |
  10. |    5    110   10485.966 |
      |-------------------------|
  11. |    7     10   5240.1692 |
  12. |    7      9   7148.0657 |
  13. |    7    501   8312.9357 |
  14. |    7      8   8761.7633 |
  15. |    7    494   11348.099 |



		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com
*
*   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index