Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: sum of count using a loop


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: sum of count using a loop
Date   Tue, 30 Jul 2013 22:58:10 +0100

See also -tabm-. -tabm- is in package -tab_chi- (SSC).

With Sergiy's dataset

. tabm cows goats horses

           |              values
  variable | [1] small  [2] regul  [3] large |     Total
-----------+---------------------------------+----------
      cows |         3          1          1 |         5
     goats |         2          3          0 |         5
    horses |         2          1          2 |         5
-----------+---------------------------------+----------
     Total |         7          5          3 |        15

. tabm cows goats horses, row

+----------------+
| Key            |
|----------------|
|   frequency    |
| row percentage |
+----------------+

           |              values
  variable | [1] small  [2] regul  [3] large |     Total
-----------+---------------------------------+----------
      cows |         3          1          1 |         5
           |     60.00      20.00      20.00 |    100.00
-----------+---------------------------------+----------
     goats |         2          3          0 |         5
           |     40.00      60.00       0.00 |    100.00
-----------+---------------------------------+----------
    horses |         2          1          2 |         5
           |     40.00      20.00      40.00 |    100.00
-----------+---------------------------------+----------
     Total |         7          5          3 |        15
           |     46.67      33.33      20.00 |    100.00


.
Nick
[email protected]


On 30 July 2013 21:27, Sergiy Radyakin <[email protected]> wrote:
> Keniajin, this code
>
> http://radyakin.org/statalist/2013073001/counts.do
>
> computes:
>
>                 v |      Freq.     Percent        Cum.
> ------------------+-----------------------------------
>   [1] small owner |          7       46.67       46.67
> [2] regular owner |          5       33.33       80.00
>   [3] large owner |          3       20.00      100.00
> ------------------+-----------------------------------
>             Total |         15      100.00
>
> From data like this:
>
>     idcode                cows               goats              horses
>          1   [2] regular owner     [1] small owner     [3] large owner
>          2     [3] large owner     [1] small owner     [1] small owner
>          3     [1] small owner   [2] regular owner     [1] small owner
>          4     [1] small owner   [2] regular owner   [2] regular owner
>          5     [1] small owner   [2] regular owner     [3] large owner
>
> You will extend it to 32 products from current 3 animal types and you
> will have 4 options for sources of products instead of three
> categories of livestock ownership. If this is not what you wanted
> please be somewhat more clear on the setup and the goal.
>
> Best, Sergiy Radyakin
>
>
> On Tue, Jul 30, 2013 at 10:12 AM, Keniajin Wambui <[email protected]> wrote:
>> I have a data set with a unique pid and a list of 32 variables with
>> the same suffix i.e maize_source, peas_source, sukuma_source,
>> cabbage_source.....
>> All the 32 variables have  option 1,2,3 or 4. I want to do a sum of
>> all the counts of each option for across all the variables using stata
>> 12 on windows 7
>> i.e
>> option    total
>> 1           4000
>> 2             568
>> 3          1200
>> 4          14000
>>
>> I am trying
>>
>> foreach var of varlist  maize_source - peas_source {
>> count if `var'==1
>>     replace total=_N+_n if `var'==1
>> countif `var'==1
>>         }
>> but its giving a count with a wrong total
>>
>> I will appreciate the support
>> --
>> Mega Six Solutions
>> Web Designer and Research Consultant
>> Kennedy Mwai
>> 25475211786
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index