Announcement

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

  • Joining frequency and percentage in same column

    Any idea on how one can join the frequency and percentage into one column and order the row labels in descending order of values, using the table command.

    Code:
    table (ballpoint) (progexp) , statistic(freq) statistic(percent, across(ballpoint)) totals(progexp) nformat(%5.1f) sformat("(%s%%)" freq percent)
    collect recode result count = frequency
    collect label levels ballpoint progexp "N", modify
    collect layout (ballpoint) (progexp#result)
    
    
    
    --------------------------------------------------------
              |                    progexp                  
              |            0                      1         
              |  Frequency    Percent   Frequency    Percent
    ----------+---------------------------------------------
    ballpoint |                                             
      0       |       61.0    (75.3%)        13.0    (68.4%)
      1       |       20.0    (24.7%)         6.0    (31.6%)
      Total   |       81.0   (100.0%)        19.0   (100.0%)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(ballpoint progexp)
    1 0
    1 0
    1 0
    1 1
    0 0
    1 0
    0 0
    1 1
    1 0
    1 0
    1 0
    0 0
    0 0
    0 1
    1 0
    1 0
    0 0
    0 0
    0 1
    1 0
    0 0
    0 1
    0 0
    1 0
    0 0
    0 0
    0 0
    1 1
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 1
    0 1
    0 1
    1 1
    0 0
    1 0
    0 0
    0 1
    0 1
    0 1
    0 1
    0 1
    1 1
    1 1
    0 1
    0 1
    1 0
    0 0
    0 0
    1 0
    1 0
    1 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    1 0
    0 0
    1 0
    1 0
    1 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    0 0
    end
    Last edited by Kehinde Atoloye; 05 May 2023, 17:02.

  • #2
    You can place multiple results in a single cell by defining a composite result, see help collect composite.

    Also collect layout supports dimension levels, and respects the order you specify them. Or you can define the automatic levels for a given dimension using collect style autolevels.

    Here are these commands in action, building on the above example:
    Code:
    . collect composite define both = frequency percent
    
    . collect style autolevels result both, clear
    
    . collect style autolevels ballpoint 1 0 .m, clear
    
    . collect preview
    
    --------------------------------------------------------
              |                    progexp                  
              |                     0                      1
              |  Frequency (Percent%)   Frequency (Percent%)
    ----------+---------------------------------------------
    ballpoint |                                             
      1       |            20 (24.7%)              6 (31.6%)
      0       |            61 (75.3%)             13 (68.4%)
      Total   |           81 (100.0%)            19 (100.0%)
    --------------------------------------------------------

    Comment


    • #3
      Originally posted by Jeff Pitblado (StataCorp) View Post
      You can place multiple results in a single cell by defining a composite result, see help collect composite.

      Also collect layout supports dimension levels, and respects the order you specify them. Or you can define the automatic levels for a given dimension using collect style autolevels.

      Here are these commands in action, building on the above example:
      Code:
      . collect composite define both = frequency percent
      
      . collect style autolevels result both, clear
      
      . collect style autolevels ballpoint 1 0 .m, clear
      
      . collect preview
      
      --------------------------------------------------------
      | progexp
      | 0 1
      | Frequency (Percent%) Frequency (Percent%)
      ----------+---------------------------------------------
      ballpoint |
      1 | 20 (24.7%) 6 (31.6%)
      0 | 61 (75.3%) 13 (68.4%)
      Total | 81 (100.0%) 19 (100.0%)
      --------------------------------------------------------
      Thanks. Is it possible to reorder the row levels and column levels without specifying the labels or values explicitly like you did here:
      Code:
      collect style autolevels ballpoint 1 0 .m, clear

      Comment


      • #4
        It is not clear what you are asking for. Do you mean something like an option to "reverse" the order of the levels? There is no such option.

        Comment

        Working...
        X