0

so I'm trying to convert a F1 Laptime that is written in a chr in to time which I can then plot into a histogram.

This is what i tried. But with no success.

lapTimes <- lapTimes %>% mutate(Time = ms(Time))
format(as.POSIXct(lapTimes$time, tz = ""), format = "%M:%S.%OS")

The time always looks like this 1:11.111, with minutes first then secunds and then milliseconds. If anyone has a idea I would greatly apprichiate that.

Thanks in advance! :D

2 Answers2

1

As stated previously, I am assuming your data looks something like this:

laptime <- c("1:11.111", "2:02.2222")

What this represents is a time interval not a date time. As such you can convert this to a difftime class and then to numeric if needed.

as.difftime(laptime, format = "%M:%S.%OS")
#Time differences in mins
#[1] 1.183333 2.033333
emilliman5
  • 5,816
  • 3
  • 27
  • 37
0

since you provided no example data, I assumed it is stored as a character.

laptime <- "1:11.111"
> as.POSIXlt.character(laptime, format = "%M:%S.%OS", tz = 'GMT')
[1] "2021-01-14 00:01:11 GMT"

# compute time difference from dates
laptime <- "1:11.111"
t2 <- as.POSIXlt.character(laptime, format = "%M:%S.%OS", tz = 'GMT')
t1 <- as.Date(t2)

> difftime(t2, t1)
Time difference of 1.183333 mins

You could also take a look at this link, looks very useful for your specific problem: https://rstudio-pubs-static.s3.amazonaws.com/276999_042092be8e31414f82ef4f41e31fe5c8.html

tester
  • 1,662
  • 1
  • 10
  • 16