如何在Stata中重塑长而宽的数据? [英] How to reshape long to wide data in Stata?
本文介绍了如何在Stata中重塑长而宽的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据:
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
I想要将上述长数据格式更改为以下宽数据。
I would like to change the above long data format into following wide data.
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)
错误,因为在测试
中没有唯一值。
It gives error because there are no unique values within tests
.
该问题的解决方案是什么?
What would be the solution to this problem?
推荐答案
您需要创建一个额外的标识符以使重复项与众不同。
You need to create an extra identifier to make replicates distinguishable.
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
另请参见此处以获取文档。
See also here for documentation.
这篇关于如何在Stata中重塑长而宽的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文