2017-08-28 29 views
0

我有数据集显示人们在10分钟的时间间隔内花了30分钟的时间。将每人累计变量扩展到时间间隔变量

Person  cumulative_time Activity 
A    10    Game 
A    30    Eat 
B    10    Sleep 
B    20    Game 
B    30    Sleep 

which means person A did gaming during the first 10 minutes, 
and eating during the next 20 minutes, 
and person B was sleeping for the first 10 min, 
gaming for the next 10 min, and sleeping for the last 10 mins. 

我想重构数据集。每一行将是每个独特的人。

然后,每列将会是这样的每个时间间隔。

Person   time10 time20   time30 
A    Game   Eat   Eat 
B    Sleep  Game   Sleep 

我知道我可以使用“崩溃”使人独特,但我不知道这可以如何用于我的目的。 “重塑”命令做了类似的事情,但我再也找不出如何使用它来做我想做的事情。

回答

1

重塑是解决这个问题的方法。像这样的东西可能会完成你所需要的。

clear 
input str1 Person int cumulative_time str8 Activity 
A    10    Game 
A    30    Eat 
B    10    Sleep 
B    20    Game 
B    30    Sleep 
end 
rename Activity time 
reshape wide time, i(Person) j(cumulative_time) 
replace time20 = time10 if missing(time20) 
replace time30 = time20 if missing(time30) 
list, clean 

如果您的问题有许多cumulative_time值,而不仅仅是三个,我会以不同的方式解决缺失值的问题。

+0

谢谢您的回答!但是你的代码的结果与我想要的结果有所不同。它应该是游戏,吃,吃。不是游戏游戏吃 – user42459

+0

您可以自由地对我的代码进行必要的微小更改以使其满足您的需求 - 读取代码并找出命令执行的操作,然后修复它们。 – 2017-08-28 21:50:49

1

除了威廉Lisowski答案,这里是用tssettsfill命令的方法:

clear 
input str1 Person int cumulative_time str8 Activity 
A    10    Game 
A    30    Eat 
B    10    Sleep 
B    20    Game 
B    30    Sleep 
end 
rename Activity time 

egen id = group(Person) 
tsset id cumulative_time, delta(10) 
tsfill, full 

bysort id : replace Person = Person[_n-1] if Person=="" 
bysort id : replace time= time[_n+1] if time=="" 
drop id 

reshape wide time, i(Person) j(cumulative_time) 
list, clean 

,输出:

 Person time10 time20 time30 
    1.  A  Game  Eat  Eat 
    2.  B Sleep  Game Sleep