所以我有以下数据集。正如您在Winner Race Time列中所看到的,仅提供获胜马的时间。我需要开发一个例程来计算剩下的时间。我是R的新手,无法提出任何代码。对此的计算是:
Length(L) = 2.4385meters
Distance * 1000meters = Distance(meters) / Length(L) = Distance(L)
Winner Race Time(sec) / Distance(L) = Seconds Per Length
Distance(L) + Behind(L) * Seconds Per Length = FINISH TIME FOR THE HORSE(sec)
列BEHIND给出了前一匹马(行)后面的长度
我希望这是可以理解的。请在回答时保持基本,谢谢! http://i.stack.imgur.com/PQTSc.png
答案 0 :(得分:0)
我写了这个基本脚本,但是用胜利者的速度来估计其他对手的最后时间有点令人困惑。我宁愿选择每个对手的速度。
#install data.table package
install.packages("data.table")
#load library data.table
library(data.table)
#building data set
distances<-c(rep(2.375, 11), rep(2,3), rep(3, 5))
raceTypes<-c(rep("Chase", 11), rep("Hurdle", 8))
positions<-c("1st", "2nd", "3rd", "4th", "5th", "6th", "7th",
"8th", "fell", "u.r.", "fell", "1st", "2nd", "3rd",
"1st", "2nd", "3rd", "4th", "5th")
behinds<-c("", "2.25L", "13L", "0.5L", "3.75L", "3.5L", "19L", "shd", "","","","","11L", "13L", "","0.5L", "2.25L", "5L", "1L")
winnerRaceTimes<-c(rep(290.9, 11), rep(238.9, 3), rep(366.2, 5))
#Distance * 1000meters = Distance(meters) / Length(L) = Distance(L)
distances<-(distances*1000)/2.4385
#I set behind to 0 for 1st
behinds[positions=="1st"]<-0
#I set behind to NA to the candidates with no position
behinds[positions=="fell"|positions=="u.r."|behinds=="shd"]<-NA
#I remove this L from behind column
behinds<-gsub("L", "", behinds)
data<-data.table(distance=distances,
raceType=raceTypes,
position=positions,
behind=behinds,
winnerRaceTime=winnerRaceTimes
)
#number of candidates per race
candidateCount<-diff(c(which(positions=="1st"), (length(positions)+1)))
#I give a race identification number to each race based on the 1st position
#I hope there is a winner for each race
data<-cbind(data, raceIdentification=unlist(lapply(1:length(candidateCount), function(x){rep(x, candidateCount[x])})))
#I compute the distance between the winner and each other candidate
data<-cbind(data, totalBehind=unlist(lapply(unique(as.numeric(data$raceIdentification)), function(raceId){lapply(1:nrow(data[raceIdentification==raceId]), function(x){sum(as.numeric(data$behind[1:x]))})})))
#Winner Race Time(sec) / Distance(L) = Seconds Per Length
#Distance(L) + Behind(L) * Seconds Per Length = FINISH TIME FOR THE HORSE(sec)
data<-data[, finalTiming:=(totalBehind/2.4385+distance)*(winnerRaceTime/distance)]
print(data)