0

New user of R here so forgive my ignorance but I've hit a bit of a bind!

I am currently working on a data set called Q1 which has a column for Start_Time that is displaying HH:MM except its vector is CHR and not INT or NUM.

I am currently trying to convert it to an INT or NUM so that I may change its format to HH:MM:SS but am having trouble doing so.

For Example:

Date <- c("11/22/22", "11/23/22", "11/24/22", "11/25/22")
Start_Time <- c("12:30", "14:25", "09:45", "17:50")
Q1 <- data.frame(Date, Start_Time)

So far I have tried

 strptime(Q1$Start_Time, format = "%H:%M:%S")

 as.integer(Q1$Start_Time)

 Q1 <- transform(Q1, Start_Time = as.integer(Start_Time))

 Q1 <- transform(Q1, Start_Time = as.numeric(Start_Time))

But all of these have returned a value of NA through my whole column or given me an error about NAs introduced by coercion.

Does anyone have any ideas? Any help would be greatly appreciated! Thank You!

malcorl
  • 11
  • 1
  • Welcome to Stack Overflow! Can you please edit your question into an minimal reproducible example to make it easier for others to help? – Mohamed Desouky Jun 01 '22 at 22:53
  • ... for example if you can edit your question with the results of `dput(Q1$Start_Time[1:5])` please – user20650 Jun 01 '22 at 22:54
  • You could try `sapply(strsplit(Q1$Start_Time, ":"), function(x) 60 * x[1] + x[2])` to get the time as an integer number of minutes. – Allan Cameron Jun 01 '22 at 23:06
  • Or `strptime(paste(Q1$Date, Q1$Start_Time), format = "%m/%d/%y %H:%M")` to convert to proper date-time format – Allan Cameron Jun 01 '22 at 23:09
  • @AllanCameron This produced the error "Error in 60 * x[1] : non-numeric argument to binary operator" the second one produced an NA response – malcorl Jun 01 '22 at 23:15
  • check the solution [here](https://stackoverflow.com/questions/72324802/r-how-to-convert-a-character-to-a-numeric-value-without-creating-nas-or-nans-wh/72325182#72325182) – Onyambu Jun 01 '22 at 23:23
  • @onyambu funny enough, I think the person in that question is using the same Case Study as me although its a different dataset. Unfortunately the as.numeric(data.table::as.ITime(Q1$Start_Time)) formula that I used still returned an NA – malcorl Jun 01 '22 at 23:35
  • I just ran `as.numeric(data.table::as.ITime(Q1$Start_Time))` and got `[1] 45000 51900 35100 64200` No `NA` values – Onyambu Jun 01 '22 at 23:39
  • Sorry, that should be `sapply(strsplit(Q1$Start_Time, ":"), function(x) 60 * as.numeric(x[1]) + as.numeric(x[2]))` – Allan Cameron Jun 01 '22 at 23:43
  • sorry @onyambu I just retried it in my dataset and it worked! You're a life saver thank you! – malcorl Jun 01 '22 at 23:46

1 Answers1

0

as user @onyambu pointed out this question was answered by them here

But to also sum it up using as.ITime from the data.table worked perfectly

as.numeric(data.table::as.ITime(Q1$Start_Time))

and returned the values for

45000 51900 35100 64200

For anyone curious about converting it back to a HH:MM:SS format I used mutate

Q1 <- Q1 %>% mutate(Start_Time = hms::hms(Start_Time))

Thanks to everyone who pitched in!

malcorl
  • 11
  • 1