我有一个足球/足球机会创建数据框,我想在每个镜头添加游戏状态,即拍摄的团队是-1,0,+ 1,+ 2 ....
数据框可以简化如下,最后一列df$Gamestate
是我想要创建的:
df<-data.frame(Gameid=c("123","123","123","123","324","324","324","324"),
Hometeam=c("A","A","A","A","C","C","C","C")
Awayteam=c("B","B","B","B","D","D","D","D")
Time=c(12,23,34,64,2,24,28,36),
Team=c("A","B","A","B","C","D","D","C"),
Goal=c(1,0,1,0,0,1,1,0),
Gamestate=c(0,-1,1,-2,0,0,1,-2))
在这里,用简单的英语,第12分钟比赛第12次由A队在第12分钟进行,所以比赛状态为零,A队得分。然后B队出手但是已经失球,所以比赛状态是-1等......
编辑:
我的想法是创建df$Scorehometeam
和df$Scoreawayteam
来跟踪每个事件的得分,并根据拍摄者填充df$gamestate
。只是不知道如何应对Gameid
和Time
答案 0 :(得分:1)
这不是最好的解决方案,但它有效:
get_score<-function(x,y,z){
n<-length(x)
s<-rep(0,n)
for (i in 1:n){
s[i]=sum(x[1:(i-1)]*(y[1:(i-1)]==y[i])*(z[1:(i-1)]==z[i]))-sum(x[1:(i-1)]*(y[1:(i-1)]!=y[i])*(z[1:(i-1)]==z[i]))
}
s[1]=0
s
}
dt$score<-get_score(dt$Goal,dt$Team,dt$Gameid)