使用Stata制表两个字符串变量

时间:2014-09-08 05:16:47

标签: stata

我的数据(只是一个例子):

choice1      choice2
--------------------
apple        mango
banana       apple
mango        apple
mango        banana       
apple        banana

我可以使用什么Stata命令获得以下结果,其中choice1choice2的列表合并在一起?

choice   number
---------------
apple      4
banana     3
mango      3

choice1choice2是两个独立的字符串变量。

1 个答案:

答案 0 :(得分:3)

这是Statalist(www.statalist.org)上讨论的标准问题。一种解决方案是使用SSC上的tabm包中的用户编写的程序tab_chi,您应该安装

ssc inst tab_chi 

您的示例很容易使用它制表。另一种方法是至少暂时重组数据:

. clear 

. input str6 choice1 str6 choice2

       choice1    choice2
1. "apple"        "mango"
2. "banana"       "apple"  
3. "mango"        "apple"
4. "mango"        "banana"       
5. "apple"        "banana"
6. end 

. tabm choice1 choice2, transpose 

            |       variable
     values |   choice1    choice2 |     Total
 -----------+----------------------+----------
      apple |         2          2 |         4 
     banana |         1          2 |         3 
      mango |         2          1 |         3 
 -----------+----------------------+----------
      Total |         5          5 |        10 


. d

Contains data
 obs:             5                          
vars:             2                          
size:            80 (99.9% of memory free)
-----------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-----------------------------------------------------------------------------
choice1         str6   %9s                    
choice2         str6   %9s                    
-----------------------------------------------------------------------------
Sorted by:  
Note:  dataset has changed since last saved

. preserve 

. stack choice1 choice2, into(choice) clear 

. tab choice 

      choice |      Freq.     Percent        Cum.
 ------------+-----------------------------------
       apple |          4       40.00       40.00
      banana |          3       30.00       70.00
       mango |          3       30.00      100.00
 ------------+-----------------------------------
       Total |         10      100.00

 . restore 

 . d 

 Contains data
  obs:             5                          
 vars:             2                          
 size:            80 (99.9% of memory free)
 -----------------------------------------------------------------------------
               storage  display     value
 variable name   type   format      label      variable label
 -----------------------------------------------------------------------------
 choice1         str6   %9s                    
 choice2         str6   %9s                    
 -----------------------------------------------------------------------------
 Sorted by:  
 Note:  dataset has changed since last saved

在数据库中搜索提及tabm的线程是了解更多信息的一种方法。在Stata中search multiple responses了解其他解决方案和讨论。