bencampbell_14
bencampbell_14

Reputation: 607

How to convert a string to a specific time format in R?

I have a dataset with 40 million rows. The goal is to clean it up starting with combining the Date and Time column into one. I was able to convert the Data from a factor to data using lubridate. This time i want to do the same to the Time column, so i can combine them in 1 column as date time using ymd_hms() and paste(). However, I'm having problems with the time column.

Here is a sample content of the said column:

sample_time_as_string <- c("18:00", "12:57","07:54:40", "17:59:02","01:00" )

The goal is to convert this string to hh:mm:ss so for example, "18:00" should become "18:00:00" and "17:59:02" should stay the same. In that way it would be easy to combine date and time columns.

This is what I've done so far using lubridate

sample_time_applied_lubridate <- hms(sample_time_as_string)

Warning message:
In .parse_hms(..., order = "HMS", quiet = quiet) :
Some strings failed to parse, or all strings are NAs

sample_time_applied_lubridate
[1] NA           NA           "7H 54M 40S" "17H 59M 2S" NA  

I tried a different function

sample_time_applied_lubridate <- hm(sample_time_as_string)

Same error

Warning message:
In .parse_hms(..., order = "HM", quiet = quiet) :
Some strings failed to parse, or all strings are NAs

sample_time_applied_lubridate
[1] "18H 0M 0S"  "12H 57M 0S" NA           NA           "1H 0M 0S"

I tried parse_date_time

sample_time_applied_lubridate <- parse_date_time(sample_time_as_string, orders = "HMS")

Warning message:
3 failed to parse. 

Do you have any more suggestion on what I can try? Maybe lubridate is not the right approach for this one?

Upvotes: 0

Views: 1435

Answers (2)

Frank Zhang
Frank Zhang

Reputation: 1688

try pass two format into orders

parse_date_time(c("18:00", "12:57","07:54:40", "17:59:02","01:00" ), orders = c("HM","HMS")) %>% 
  format("%H:%M:%S")

Upvotes: 2

Edward
Edward

Reputation: 19494

You can use the str_pad function from stingr to pad the times without seconds.

library(stringr)
ifelse(nchar(sample_time_as_string)==5,
            str_pad(paste0(sample_time_as_string, ":"), width=8, side="right", pad="0"), 
       sample_time_as_string)

#[1] "18:00:00" "12:57:00" "07:54:40" "17:59:02" "01:00:00"

Upvotes: 0

Related Questions