【问题标题】:Stata: Transposing panel rows to columnStata:将面板行转换为列
【发布时间】:2018-03-26 19:43:22
【问题描述】:

我正在尝试将以下面板数据集重新排列为可以与另一个合并的表单。我想改变这个:

Gender  Year  IndA  IndB  IndC
   1    2008  0.22  0.34  0.45
   2    2008  0.78  0.66  0.55
   1    2009  0.25  0.36  0.49
   2    2009  0.75  0.64  0.51
   1    2010  0.28  0.38  0.48
   2    2010  0.72  0.62  0.52

进入:

(ID)  Year  Industry    1    2
  1   2008     A      0.22  0.78
  2   2009     A      0.25  0.75
  3   2010     A      0.28  0.72 
  4   2008     B      0.34  0.66
  5   2009     B      0.36  0.64
  6   2010     B      0.38  0.62
  7   2008     C      0.45  0.55
  8   2009     C      0.49  0.51
  9   2010     C      0.38  0.62

我是 Stata 的新手,在重塑列和性别时遇到了困难。

【问题讨论】:

  • 感谢您的所有回复 - 非常有帮助,并且已成功实现上述目标。

标签: panel stata


【解决方案1】:

见help reshape。一种方法是连续重塑。您可以执行第一行,在数据浏览器中查看数据,然后执行第二行,看看它是如何工作的。您还需要为最终变量选择 1 和 2 以外的名称。

    reshape long Ind, i(Year Gender)   j(Industry) string
    reshape wide Ind, i(Year Industry) j(Gender)

【讨论】:

    【解决方案2】:

    您也可以用stack 替换第一个整形(不太清晰,但有时比reshape 更快):

    stack Gender Year IndA Gender Year IndB Gender Year IndC, into(Gender Year Y) clear
    rename _stack Industry
    lab define Industry 1 "A" 2 "B" 3 "C"
    lab val Industry Industry
    
    reshape wide Y, i(Industry Year) j(Gender)
    sort Industry Year
    gen id = _n
    order id Year Industry
    list, sepby(Industry) noobs
    

    【讨论】:

      【解决方案3】:

      作为同一主题的第三个变体,请注意两个性别的比例之和为 1,因此我们只需要一个。

        clear 
        input Gender  Year  IndA  IndB  IndC
         1    2008  0.22  0.34  0.45
         2    2008  0.78  0.66  0.55
         1    2009  0.25  0.36  0.49
         2    2009  0.75  0.64  0.51
         1    2010  0.28  0.38  0.48
         2    2010  0.72  0.62  0.52
        end 
        drop if Gender == 1 
        drop Gender 
        reshape long Ind , i(Year) j(Type) string 
      
        list , sepby(Year) 
      
           +-------------------+
           | Year   Type   Ind |
           |-------------------|
        1. | 2008      A   .78 |
        2. | 2008      B   .66 |
        3. | 2008      C   .55 |
           |-------------------|
        4. | 2009      A   .75 |
        5. | 2009      B   .64 |
        6. | 2009      C   .51 |
           |-------------------|
        7. | 2010      A   .72 |
        8. | 2010      B   .62 |
        9. | 2010      C   .52 |
           +-------------------+
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-25
        • 2021-05-21
        相关资源
        最近更新 更多