I have a field in a R dataframe, say called OriginTime. The field has basically two types of values, read in from several excel files and rbinded together. Example df follows:
OriginTime <- c(0.777888, 0.999888, 0.25, 0.3755, "12:24", "05:59")
The entire column is character values. I want to convert the entire column to time field in the format hh:mm. But whenever I attempt to convert the column values, say using chron::times, the rows with an already hh:mm format get turned into NA values, and the decimal values are correctly reformated. I have attempted various methods to resolve but to no avail. One example
ifelse(substr(OriginTime,3,3)!=":",chron::times(as.numeric(as.character(OriginTime))),OriginTime)
It seems
chron::timesis very picky about the time format. It complains and says it's expecting 3 items "h:m:s" when converting "12:24".The following solution transforms numeric values separately from character values of type "h:m:s" and then combines the result. I tried doing it on one pass with an if statement like you tried, but the resulting vector did not retain the time format.
Edit: I realize you can do it on one pass with this