3

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

3

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
Roland
  • 127,288
  • 10
  • 191
  • 288
0

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})