Skip to content

Instantly share code, notes, and snippets.

@durtal
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durtal/2834597685162e5dc266 to your computer and use it in GitHub Desktop.
Save durtal/2834597685162e5dc266 to your computer and use it in GitHub Desktop.
FUNCTIONS FOR CLEANING TRAKUS TIMES ### The link below walks through and explains the functions (http://protractedcontemplation.wordpress.com/2014/01/29/r-code-working-trakus-sectionals-for-meydan/)
cleanup <- function(racedata){
for(i in 4:length(racedata)){
racedata[,i] <- sapply(strsplit(racedata[,i], "[[]"), function(x) x[1])
}
toSeconds <- function(x){
unlist(
lapply(x, function(y){
y <- as.numeric(strsplit(y, ":", fixed=T)[[1]])
if(length(y)==2)
y[1]*60 + y[2]
else if(length(y)==1)
y[1]
}))
}
for(i in 4:length(racedata)){
racedata[,i] <- toSeconds(racedata[,i])
}
racedata <- racedata[,-2]
return(racedata)
}
newnames <- function(racedata, Distance){
if(Distance==1000){
names(racedata) <- c("Pos", "Horse", "c200", "c400", "c600", "c800",
"c1000")
} else if(Distance==1200){
names(racedata) <- c("Pos", "Horse", "c200", "c400", "c600", "c800",
"c1000", "c1200")
} else if(Distance==1400){
names(racedata) <- c("Pos", "Horse", "c200", "c400", "c600", "c800",
"c1000", "c1200", "c1400")
} else if(Distance==1600){
names(racedata) <- c("Pos", "Horse", "c200", "c400", "c600", "c800",
"c1000", "c1200", "c1400", "c1600")
} else if(Distance==1800){
names(racedata) <- c("Pos", "Horse", "c200", "c400", "c600", "c800",
"c1000", "c1200", "c1400", "c1600", "c1800")
} else if(Distance==1900){
names(racedata) <- c("Pos", "Horse", "c400", "c600", "c800", "c1000",
"c1200", "c1400", "c1600", "c1800", "c1900")
} else if(Distance==2000){
names(racedata) <- c("Pos", "Horse", "c400", "c600", "c800", "c1000",
"c1200", "c1400", "c1600", "c1800", "c2000")
} else if(Distance==2200){
names(racedata) <- c("Pos", "Horse", "c400", "c800", "c1000", "c1200",
"c1400", "c1600", "c1800", "c2000", "c2200")
} else if(Distance==2435){
names(racedata) <- c("Pos", "Horse", "c400", "c800", "c1200", "c1400",
"c1600", "c1800", "c2000", "c2200", "c2435")
} else if(Distance==2810){
names(racedata) <- c("Pos", "Horse", "c400", "c800", "c1200", "c1600",
"c2000", "c2200", "c2400", "c2600", "c2810")
} else { stop("Invalid Distance: Amend function")}
racedata$Horse <- tolower(racedata$Horse)
return(racedata)
}
raceinfo.df <- function(racedata, date, race.no, dist, purse=NA, surface=NA, gate=TRUE, merge=TRUE){
rnrs <- length(racedata[,1])
daterace <- paste(date, "_", race.no, sep="")
race <- rep(race.no, rnrs)
Gate <- rep(NA, rnrs)
df <- data.frame(Date=date, RaceNo.=race.no, Date_Race=daterace, Dist=dist)
if(is.na(purse)){ df <- df} else { df <- cbind(df, Purse=purse)}
if(is.na(surface)){ df <- df} else { df <- cbind(df, Surf=surface)}
if(gate==TRUE){
df <- cbind(df, Gate=NA)
} else if(gate==FALSE){
df <- df
}
if(merge==FALSE){
newdatadf <- df
} else if (merge==TRUE){
newdatadf <- cbind(df, racedata)
}
return(newdatadf)
}
fs.extras <- function(racedata, Dist, ISects=FALSE, Trip=FALSE, Pos=FALSE, merge=TRUE){
dists <- c()
firstcol <- which(names(racedata)=="Horse") + 1
lastcol <- which(names(racedata)==paste("c", Dist, sep=""))
for(i in (firstcol):(lastcol)){
dists <- append(dists, sapply(strsplit(names(racedata)[i], "c"), function(x) x[2]))
}
FinSpd.df <- racedata[,(firstcol):(lastcol - 1)]
finaltimes <- racedata[,lastcol]
FinSpd.df <- finaltimes - FinSpd.df
findists <- Dist - as.numeric(dists[1:(length(dists)-1)])
for(i in 1:(length(dists)-1)){
FinSpd.df[,i] <- round(((finaltimes * findists[i]) * 100 / (FinSpd.df[,i] * Dist)) / 100, 3)
}
names(FinSpd.df) <- paste("fs", as.character(findists), sep="")
ISects.df <- racedata[,(firstcol):(lastcol)]
names(ISects.df) <- paste("i", dists, sep="")
l.df <- length(ISects.df)
for(i in l.df:2){
ISects.df[,i] <- ISects.df[,i] - ISects.df[,i-1]
}
Pos.df <- racedata[,(firstcol):(lastcol)]
for(i in 1:length(Pos.df)){
Pos.df[,i] <- as.numeric(factor(Pos.df[,i]))
}
names(Pos.df) <- paste("p", dists, sep="")
Trip.df <- ISects.df
names(Trip.df) <- paste("d", dists, sep="")
n <- names(Trip.df)
for(i in 1:length(n)){
Trip.df[,n[i]] <- NA
}
if(ISects==TRUE){
newdf <- cbind(FinSpd.df, ISects.df)
} else { newdf <- FinSpd.df }
if(Pos==TRUE){
newdf <- cbind(newdf, Pos.df)
} else { newdf <- newdf }
if(Trip==TRUE){
newdf <- cbind(newdf, Trip.df)
} else { newdf <- newdf }
if(merge==TRUE){
return(cbind(racedata, newdf))
} else { return(newdf)}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment