我想创建一个新的变量,如果发生事件,那么我想回顾所有先前基于时间变量1的事件。我有一些下面的示例数据。我很迷茫,不知道该从哪里开始。基于R中的滞后观察值创建一个变量
event<-c("Dribble","Pass","Dribble","Bad Shot","Shot Miss","Rebound","Pass","Pump Fake","Good Shot","Shot Miss")
time<-c(1,2,3,4,5,6,6.5,6.9,6.92,6.95)
player_id<-c(1,1,2,2,2,1,1,2,2,2)
pass_to_shot<-c("","Pass to Shot","","","","","Pass to Shot","","","")
test_data<-data.frame(player_id,event,time,pass_to_shot)
player_id event time pass_to_short
1 Dribble 1 NA
1 Pass 2 Pass to Shot
2 Dribble 3 NA
2 Bad Shot 4 NA
2 Shot Miss 5 NA
1 Rebound 6 NA
1 Pass 6.5 Pass to Shot
2 Pump Fake 6.9 NA
2 Good Shot 6.92 NA
我想它是这个样子:
player_id event time pass_to_short chance_create
1 Dribble 1 NA
1 Pass 2 Pass to Shot
2 Dribble 3 NA
2 Bad Shot 4 NA
2 Shot Miss 5 NA
1 Rebound 6 NA
1 Pass 6.5 Pass to Shot 1
2 Pump Fake 6.9 NA
2 Good Shot 6.92 NA
我真的不明白如何引用过去的观察中R数据集。基本上如果event ==“Pass”并且在接下来的1秒内有一个“Good Shot”事件(单位为时间),那么我希望chance_create等于1.任何帮助都会很棒,谢谢!