How to convert a string
t <- c("00:00:0.00", "00:00:0.34")
in number? tried several approaches - but none of them worked.
The basic idea is to convert your character string to a valid POSIX*t object, and then convert it to a numeric value:
POSIX*t
numeric
## Set a couple of printing options options(digits = 12) options(digits.secs = 3) ## Convert from character to POSIXlt to numeric (a <- strptime(t, format="%H:%M:%OS", tz="GMT")) # [1] "2013-04-09 00:00:00.00 GMT" "2013-04-09 00:00:00.34 GMT" (b <- as.numeric(a)) # [1] 1365465600.00 1365465600.34
Please note that when converting from numeric to POSIX*t there are floating point problems that can change how these objects print. ( See here for a more detailed discussion of this issue. )
## It _looks_ like you've lost 1/100 second on the second time object (c <- as.POSIXct(as.numeric(b), origin = "1970-01-01", tz="GMT")) # [1] "2013-04-09 00:00:00.00 GMT" "2013-04-09 00:00:00.33 GMT" ## Here a workaround for nicer printing. as.POSIXct(as.numeric(b+1e-6), origin = "1970-01-01", tz="GMT") # [1] "2013-04-09 00:00:00.00 GMT" "2013-04-09 00:00:00.34 GMT"