Home > OS >  How to convert time HH:MM:SS to decimal form in R?
How to convert time HH:MM:SS to decimal form in R?

Time:10-28

I need to turn my time variable (all data formatted in HH:MM:SS) into a a numeric and decimal of the hour.

For example 07:05:00 turns into 7.083 using base R (I'm in a secure lab so can't access packages).

Is there a way to do this using base R code?

CodePudding user response:

You can easily write your own parser:

parse_time <- function(x) {
  res <- do.call(rbind, strsplit(x, ":", TRUE))
  mode(res) <- "numeric"
  c(res %*% (1/c(1, 60, 3600)))
}

parse_time(c("07:05:00","07:05:30"))
#[1] 7.083333 7.091667

CodePudding user response:

This was how I managed it in the end:

Test$Hours <- as.character(Test$Offence.Start.Time)
Test$Hours <- sapply(strsplit(Test$Hours,":"),
   function(x) {
     x <- as.numeric(x)
     x[1] x[2]/60})                                  
  • Related