0

I apologize in advance if this has been posted elsewhere. I am new to R, and I am having trouble manipulating a dataframe.

I have a column in my dataframe that contains times in the format of hh mm ss. However, some values only have a single "0" in the hh place (for example, "0:01:30" or "0:20:30").

I am trying to use the chron package to convert these into a time object so that I can find the min, max, and mean time durations, but I need to add a zero to the hours column before I do so. What would be the best way to do this in R? Since some values in the column don't need to have a zero added, what would be the best way to iterate through each value and check for a specific condition?

Thanks in advance for your help!

  • `times(c("0:01:30", "0:20:30"))` works without adding a 0. – G. Grothendieck Jun 11 '21 at 17:52
  • I went back and tried again, and I realized my error wasn't with the single zero at the end, but instead because some values were well over 24 hours. I'm trying to find a way to make this a time or numeric object so that I can find the average time duration. Would I need to find another package? – peter-j-schmidt Jun 13 '21 at 20:04
  • Try `library(lubridate); mean(as.duration(hms(c("30:01:30", "0:20:30"))))` – G. Grothendieck Jun 13 '21 at 22:54
  • That didn't seem to work either. It gave me a parse error with this message: “Some strings failed to parse, or all strings are NAs”. the values that are over 24 hours are quite large (for example, "142:30:00" and "123:21:00"). The times() function set these to NA. If those are the issue, would I have to create my own function to manipulate those? – peter-j-schmidt Jun 14 '21 at 13:46
  • I was not suggesting that this be used with `times`. It returns the number of seconds. If you want to display this as a lubridate period object use `as.period(as.duration(mean(as.duration(hms(c("142:30:00", "123:21:00"))))))` – G. Grothendieck Jun 14 '21 at 13:56

1 Answers1

0

Probably you can deal with that directly via the chron package as suggested in the comments.

Otherwise a simple if else statement should do the job. Assuming t is your column and dat your dataframe, and the issue happens only in the hh part(otherwise add more conditions):

library(dplyr)
dat$t1 <- if_else(nchar(dat$t)==7, paste0(0,dat$t), dat$t)
marqui
  • 31
  • 2