Reputation: 335
Trying to convert a time string e.g "0:05" to a hms difftime object e.g 00:05:00 in R.
Reason: trying to combine two dataframes columns, one has a column that is type "hms" "difftime", and other is type "character"
Upvotes: 2
Views: 1185
Reputation: 1826
If you want to translate a string into a difftime object, you can use lubridate
. It might be bringing in the big guns for something can easily do with as.difftme
as @thelatemail suggests, but if you are working with time units, then you want to know about lubridate
anyway.
You can translate a "05:00"
HH:MM string into a time object using lubridate:hm
an translate that into a difftime
using as.difftime
:
> print(lubridate::as.difftime(lubridate::hm("05:00")))
Time difference of 18000 secs
Upvotes: 1