R time variable conversion

Hi, I have a dataframe with a character time variable. it has values like "18:47:48" "18:52:48" "18:57:48" "19:02:48" "19:07:48" "19:12:48". I would like to convert it in to a numeric column as it is.

I did try NUM_TIME=hms(df$CHAR_TIME), but it gives me both date+time values, dont know why is it getting the date column and hms is giving it inthe form of list.

kindly help in getting a numeric time column that looks alike the character time column values mentioned above.

Try as_hms() from the hms package.

DF <- data.frame(Char_Time = c("18:47:48", "18:52:48", "18:57:48", "19:02:48", "19:07:48", "19:12:48"))
DF$Char_Time <- hms::as_hms(DF$Char_Time)
DF
#>   Char_Time
#> 1  18:47:48
#> 2  18:52:48
#> 3  18:57:48
#> 4  19:02:48
#> 5  19:07:48
#> 6  19:12:48
str(DF)
#> 'data.frame':    6 obs. of  1 variable:
#>  $ Char_Time: 'hms' num  18:47:48 18:52:48 18:57:48 19:02:48 ...
#>   ..- attr(*, "units")= chr "secs"

Created on 2023-01-17 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.