我有一个数据集:
person_id
。humanities
,IT
,business
等。)Degree
。 这看起来如下:
person_id humanities business IT Degree 1 0 1 0 BSc 1 0 0 1 MSc 2 1 0 0 PhD 2 0 1 0 MSc 2 0 0 1 BSc 3 0 0 1 BSc
我想转换这个数据集,以便我有每个person_id
的度和主题的每种可能组合的变量。
我的想法是,当我collapse
稍后person_id
时,我会为每个人创建一个值(即0
或1
)。我有十二个不同的科目和四个主要学位。
person_id humanities business IT Degree BSc_humanities MSc_Hum
1 0 1 0 BSc 0 0
1 0 0 1 MSc 0 0
2 1 0 0 PhD 0 1
2 1 0 0 MSc 0 1
2 0 0 1 BSc 0 1
3 0 0 1 BSc 0 0
实现这一目标的最佳方法是什么?
答案 0 :(得分:1)
您可以使用fillin
:
clear
input person_id humanities business IT str3 Degree
1 0 1 0 BSc
1 0 0 1 MSc
2 1 0 0 PhD
2 0 1 0 MSc
2 0 0 1 BSc
3 0 0 1 BSc
end
fillin person_id humanities business Degree
list person_id humanities business Degree
+-----------------------------------------+
| person~d humani~s business Degree |
|-----------------------------------------|
1. | 1 0 0 BSc |
2. | 1 0 0 MSc |
3. | 1 0 0 PhD |
4. | 1 0 1 BSc |
5. | 1 0 1 MSc |
|-----------------------------------------|
6. | 1 0 1 PhD |
7. | 1 1 0 BSc |
8. | 1 1 0 MSc |
9. | 1 1 0 PhD |
10. | 1 1 1 BSc |
|-----------------------------------------|
11. | 1 1 1 MSc |
12. | 1 1 1 PhD |
13. | 2 0 0 BSc |
14. | 2 0 0 MSc |
15. | 2 0 0 PhD |
|-----------------------------------------|
16. | 2 0 1 BSc |
17. | 2 0 1 MSc |
18. | 2 0 1 PhD |
19. | 2 1 0 BSc |
20. | 2 1 0 MSc |
|-----------------------------------------|
21. | 2 1 0 PhD |
22. | 2 1 1 BSc |
23. | 2 1 1 MSc |
24. | 2 1 1 PhD |
25. | 3 0 0 BSc |
|-----------------------------------------|
26. | 3 0 0 MSc |
27. | 3 0 0 PhD |
28. | 3 0 1 BSc |
29. | 3 0 1 MSc |
30. | 3 0 1 PhD |
|-----------------------------------------|
31. | 3 1 0 BSc |
32. | 3 1 0 MSc |
33. | 3 1 0 PhD |
34. | 3 1 1 BSc |
35. | 3 1 1 MSc |
|-----------------------------------------|
36. | 3 1 1 PhD |
+-----------------------------------------+