This is a guess at what you ultimately need: fractional hours. I think you want 0
and around 11.57
for those two times shown. I'm going to infer that they aren't meant to be time-of-day, just "time spent" (in hours:minutes:seconds
).
Two techniques for parsing this:
Here's a trick that relies on R's use of 1970-01-01
as "origin of unix epoch time".
as.POSIXct(paste("1970-01-01", unisim_usage_hours_may_2020$Usage.Hours), tz = "UTC")
# [1] "1970-01-01 00:00:00 UTC" "1970-01-01 11:34:20 UTC"
as.numeric(as.POSIXct(paste("1970-01-01", unisim_usage_hours_may_2020$Usage.Hours), tz = "UTC"))
# [1] 0 41660
as.numeric(as.POSIXct(paste("1970-01-01", unisim_usage_hours_may_2020$Usage.Hours), tz = "UTC")) / 3600
# [1] 0.00000 11.57222
Parse it manually, assuming that there aren't any surprised ...
strsplit(unisim_usage_hours_may_2020$Usage.Hours, ":")
# [[1]]
# [1] "00" "00" "00"
# [[2]]
# [1] "11" "34" "20"
sapply(strsplit(unisim_usage_hours_may_2020$Usage.Hours, ":"), function(a) sum(as.numeric(a) / c(1, 60, 3600)))
# [1] 0.00000 11.57222
Use whichever makes the most sense to you, since you're the one maintaining your code. (If performance matters to you, then the second is faster only with very small datasets; on my machine, the first can take half the time with only a few thousand rows of data. Granted, we're talking half-time measured in microseconds, so it's not cosmic, but if you have "large-ish" data then ... *shrug*.)
Another option (though I don't know what you're meaning to do with this):
Use the lubridate
package to convert it into a lubridate
-proprietary class:
lubridate::hms(unisim_usage_hours_may_2020$Usage.Hours)
# [1] "0S" "11H 34M 20S"
Over to you with what to do with those ... I am not well-versed in its use.
Data:
unisim_usage_hours_may_2020 <- data.frame(Usage.Hours = c("00:00:00", "11:34:20"), stringsAsFactors = FALSE)