简体   繁体   English

如何用R计算足球得分目标的时差

[英]How to use R to calculate time gaps in scoring goals in soccer

I would like to get a numeric vector of time gaps between goals scored by a soccer team 我想获得足球队得分之间的时间差距的数字向量

df <- data.frame(game=c(1,2,3,4,5,6,6,6,7),goaltime=c(NA,35,51,NA,NA,2,81,90,15))

NA indicates no goal was scored by the team in that game. NA表示球队在该比赛中没有进球。 The earliest a goal can be scored in a game is 1 最早的一个目标可以在游戏中得分为1

Each game has a total time of 90 minutes so the output vector should be 每个游戏的总时间为90分钟,因此输出矢量应为

c(125,106,221,79,9,15,75)

You can try: 你可以试试:

diff(c(0,setdiff(90*(df$game-1)+df$goaltime,NA),90*max(df$game)))
#[1] 125 106 221  79   9  15  75

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM