0

I could find any answers to that. So I've got the following code and trying to put it into apply, so it does the work quicker, my data set is 130k rows long. I need an apply that will calculate the missing times of the horses from Behind(in Length) and the winning Horse time. The problem is that the column Behind gives a the distance behind the horse before, not the first 1. So I'm in need to create a variable that will carry on as the function goes and if new race is identified, finds that the position == 1, it resets the variables.

missingTimes <- function(x) {
    L <- 2.4384
    for(i in 1:nrow(x) - 10) {
        distanceL <- (x$distance[i] * 1000) / L
        LperS <- x$Winner.Race.time[i] / distanceL

        if(x$position[i] == 1 && !is.na(x$position[i])) {
            distanceL <- NULL
            LperS <- NULL
        }

        if(grepl("L",x$Behind[i])) {
            x$results[i] <- (distanceL + as.numeric(sub("L", "", x$Behind[i]))) * LperS
        }
    }
}

I need at least 10 reputation to post images, thats why I give you links instead! https://i.stack.imgur.com/xN23M.png https://i.stack.imgur.com/Cspfr.png

The results should just give me a column with the proper times for the finish times of the other horses, in a form like the column Winner Race Time

For further understanding Imma count a few results myself for you:

Starting with first row, it sees position = 1, so it cleans the variables. Then it takes the distance * 1000, and divides it by the constant L,

2.375 * 1000 / 2.4384 = 973.99

Then It need to get the time in seconds it takes to complete 1 length(L),

290.9 / 973.99 = 0.298

Now to get the finish time for the second horse It adds the length BEHIND to the distance of the racing track and multiplies it by the length per second,

973.99 + 2.25 = 976.24 * 0.298 = 290.91952

Then for the next horses time it'd be:

976.24 + 13 = 989.24 * 0.298 = 294.79352

and so on, remember when it hits position = 1, distance needs to reset

What I've done alternatively is put the distanceL in a separate column, same with LperS, of course after calculation. If you could walk me through steps required to get that done It'd be great. I'm a complete rookie to the R stuff, so please be descriptive. I hope you catch my understanding! Thank you!

Hryniu
  • 11
  • 2
  • You need to provide data: http://stackoverflow.com/a/5963610/1412059 – Roland Nov 29 '14 at 16:41
  • https://www.dropbox.com/s/78u8eem2btd76ha/british%20results.csv?n=240455519 Sorry for that! – Hryniu Nov 29 '14 at 16:51
  • 1
    We shouldn't have to download it. Put a few lines in the post with `dput(head(data))` and also show what you want for the result – Rich Scriven Nov 29 '14 at 16:55
  • I did what you asked. If any of you would like to have some form of a IM conversation, then I'm up for it. – Hryniu Nov 29 '14 at 17:15
  • Lads.. A deadline is coming up, so I'd like someone to help me out as soon as the can. Thanks! – Hryniu Nov 29 '14 at 18:41

0 Answers0