Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Manipulating tabulation output so rows are sorted by value of column...


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Manipulating tabulation output so rows are sorted by value of column...
Date   Thu, 9 Feb 2012 23:57:21 +0000

-table- is at first sight a dead end here, as there is no such sorting option. 

Although I can imagine devious work-arounds, I think you will get most flexibility just by creating a reduced dataset and -sort-ing in exactly the way the want. 

You said increasing order, but your examples are decreasing, but you can have it either way. 

Consider

. sysuse auto, clear

. drop if missing(rep78)

. collapse mpg , by(foreign rep78)

. gsort foreign -mpg

. format mpg %2.1f

. l, sepby(foreign) noobs

  +-------------------------+
  | rep78    foreign    mpg |
  |-------------------------|
  |     5   Domestic   32.0 |
  |     1   Domestic   21.0 |
  |     2   Domestic   19.1 |
  |     3   Domestic   19.0 |
  |     4   Domestic   18.4 |
  |-------------------------|
  |     5    Foreign   26.3 |
  |     4    Foreign   24.9 |
  |     3    Foreign   23.3 |
  +-------------------------+


Nick 
n.j.cox@durham.ac.uk 

Rieza Soelaeman


Supposing I have the following data:
Name   Year    Value
AA    1990 985
AA    1990 246
AA    2000 237
AA    2000 560
BB    1990 457
BB    1990 742
BB    2000 985
BB    2000 154
CC    1990 799
CC    1990 656
CC    2000 980
CC    2000 812


And I want to see results displayed as follows (take the average of
the value for each name, and display by year, sorted by mean(value)):

YEAR==1990
Name mean(value)
CC   727.5
AA   615.5
BB   599.5

and

YEAR==2000
Name mean(value)
AA   599.5
BB   569.5
CC   357.5


How would I modify my existing code such that the results are
displayed in increasing order of mean(value),
so I can see easily which name had the highest mean(value)?

.table Name if Year==1990, contents(mean value)
.table Name if Year==2000, contents(mean value)

Is there an in-line sort option that goes with the table command?

Right now the result is not displayed in any specific way.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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   |   Site index