2

I have a dataset where the time was recorded as HH:MM and is classified as a character. I want to be able to do simple calculations like duration (i.e EndTime-StartTime), and to be able to classify the time of day that the shift happened.

I have found the Chron function that stores the times as fractions of a day, and I believe that would help me do the classifications and the simple calculations, however because my data is HH:MM rather than HH:MM:SS I am running into issues.

Is there a way for me to add :00 to the end of all of my entries OR a way to change the class of my variable to be able to do these calculations?

1 Answers1

1

Update:

Many thanks to @Rui Barradas:

to avoid using paste we could simple : lubridate::hm(your_string)!

First answer:

One possible approach is to use lubrdiates hms function in combination with paste:

your_string <- "02:04"

library(lubridate) 

hms(paste(your_string, "00"))

[1] "2H 4M 0S"
TarJae
  • 72,363
  • 6
  • 19
  • 66