简体   繁体   中英

How to convert length of time to numeric in R?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM