0

I have a data frame with the amount of time it takes to do a lap and I'm trying to separate that into individual data frames for each driver.

These time values look like this, being in minutes:seconds.milliseconds, except for the first lap which has a Colon in between seconds and milliseconds.

13:14:50 1:28.322 1:24.561 1:23.973 1:23.733 1:24.752

I'd like to have these in a separate data frame in a seconds format like this.

794.500 88.322 84.561 83.973 83.733 84.752

When I convert this to a numeric it gives the following values.

214 201 174 150 133 183

And when I use strptime or POSIXlt it gives me huge values which are also wrong, even when I use the format codes. However, I subtracted 2 values to find that the time difference was correct, and through that I found that were all off by 1609164020. Also, these values ignore the decimal values which I need.

Lachie
  • 1
  • Doesn't answer your question, but I think your interpretation of the first entry is wrong. That looks much more like a time than a duration. Does it ever have 3 digits for the milliseconds? – user2554330 Dec 29 '20 at 01:03
  • Can you provide a reproducible example of the data that you have. See here on [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Also do you really need the output in separate dataframes? So each dataframe would have only 1 value in it? – Ronak Shah Dec 29 '20 at 03:26

1 Answers1

0

You can use POSIXlt in conjunction with a conversion to seconds.

First, add a date to your first time element:

ds <- c("13:14:50", "1:28.322", "1:24.561", "1:23.973", "1:23.733", "1:24.752")
ds[1] <- paste( Sys.Date(), ds[1] )
#[1] "2020-12-29 13:14:50" "1:28.322"            "1:24.561"           
#[4] "1:23.973"            "1:23.733"            "1:24.752"

Create a function to convert the subsequent minutes:seconds.milliseconds to seconds.milliseconds:

to_sec <- function(x){ as.numeric(sub( ":.*","", x )) * 60 +
  as.numeric( sub( ".*:","", x ) ) }

Convert the vector to dates that enable calculation of time differences:

ds[2:6] <- to_sec(ds[2:6])
ds[2:6] <- cumsum(ds[2:6])

dv <- c( as.POSIXlt(ds[1]), as.POSIXlt(ds[1]) + as.numeric(ds[2:6]) )
# [1] "2020-12-29 13:14:50 CET" "2020-12-29 13:16:18 CET"
# [3] "2020-12-29 13:17:42 CET" "2020-12-29 13:19:06 CET"
# [5] "2020-12-29 13:20:30 CET" "2020-12-29 13:21:55 CET"

dv[6] - dv[1]
# Time difference of 7.089017 mins
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29