Search code examples
rchron

using chron package to format data in R


at the moment I'm trying to convert a string into time-format. e.g. my string looks like following: time <- '12:00'. I already tried to use the chron-Package. And my code looks like following:

time <- paste(time,':00', sep = '') time <- times(time)

Instead of getting a value like "12:00:00" the function times() always translate the object time into "0.5" Am I using the wrong approach?

regards


Solution

  • Your code works. If you check the 'class()' it is "times". However, if you want another way, try:

    time <- '12:00:00'
    newtime<-as.POSIXlt(time, format = "%H:%M:%S") # The whole date with time
    t <- strftime(newtime, format="%H:%M:%S") # To extract the time part
    t
    #[1] "12:00:00"
    

    Cheers !