【问题标题】:How to reshape long to wide data in Stata?如何在Stata中重塑从长到宽的数据?
【发布时间】:2015-05-04 14:05:36
【问题描述】:

我有以下数据:

id      tests      testvalue
1       A           4
1       B           5
1       C           3
1       D           3 
2       A           3
2       B           3
3       C           3
3       D           4
4       A           3
4       B           5
4       A           1
4       B           3

我想把上面的长数据格式改成下面的宽数据。

id      testA   testB    testC   testD   index
1       4      5        3         3        1
2       3      3        .         .        2
3       .      .        3         4        3
4       3      5        .         .        4
4       1      3        .         .        5

我在努力

reshape wide testvalue, i(id) j(tests) 

因为tests 中没有唯一值,所以会出错。

解决这个问题的方法是什么?

【问题讨论】:

    标签: stata data-management


    【解决方案1】:

    您需要创建一个额外的标识符以使复制品可区分。

    clear 
    input id  str1    tests      testvalue
    1       A           4
    1       B           5
    1       C           3
    1       D           3 
    2       A           3
    2       B           3
    3       C           3
    3       D           4
    4       A           3
    4       B           5
    4       A           1
    4       B           3
    end 
    bysort id tests: gen replicate = _n 
    reshape wide testvalue, i(id replicate) j(tests) string 
    

    有关文档,另请参阅 here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多