0

I have a data frame with two columns for TIME. Time has the formatting H:M:S

dataframe <- 
   TIME1        TIME2
   08:20:05     12:30:05

I want to calculate time difference in "hours" and put it in a column to data frame. I tried this but didn't work.

dataframe$TimeDif <- difftime(dataframe$TIME1, dataframe$TIME2, unit="hours")

This is the error msg I get: Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

daragh
  • 173
  • 1
  • 11
  • This is the error msg I get: `Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format` – daragh Aug 10 '17 at 06:54
  • There you go, your variables are character. You need to change it to time. Have you tried searching the internet? This is a very common question (see [here](https://stackoverflow.com/questions/12649641/calculating-time-difference-in-r) for example). Also, you can edit your question to include the error message. Perhaps take a minute and read up on how SO functions. – Roman Luštrik Aug 10 '17 at 06:56
  • I searched and tried this hut still did't work: `data$TIME1 <- as.POSIXct(data$TIME1 , format = "%H:%M:%S") data$TIME2 <- as.POSIXct(data$TIME2 , format = "%H:%M:%S") data$TimeDif <- difftime(data$TIME1 , data$TIME2 , unit="hours")` – daragh Aug 10 '17 at 07:05
  • It needs to be datetime or date. AFAIK there is no time object in R. Please study the documentation (or e.g. [here](https://www.stat.berkeley.edu/~s133/dates.html)). – Roman Luštrik Aug 10 '17 at 07:07
  • Yep got it! problem solved now. Thank you! – daragh Aug 10 '17 at 07:16

1 Answers1

0

please try the code below:

 difftime(strptime(dataframe$TIME1,"%H:%M:%S"),strptime(dataframe$TIME2,"%H:%M:%S"))

if this helps let us know. thank you

Onyambu
  • 67,392
  • 3
  • 24
  • 53