0

one simple question that is driving me crazy.. I have a time column in this format:

time
00:05:00
00:10:00
00:15:00
10:20:00
10:25:00

How can I convert all the values in time (just like the as.Date function works for dates)? I tried

df$time <- chron(df$time)

but it just convert the values into atomic ones. I also read this post but it doesn't help me.

Community
  • 1
  • 1
matteo
  • 4,683
  • 9
  • 41
  • 77
  • That question _does_ help you, assuming you actually do what the answer suggests: `chron(times=df$time)`. – Joshua Ulrich Aug 07 '13 at 15:33
  • 3
    I'm surprised you didn't get an error and several warnings when you tried it without naming the argument. – GSee Aug 07 '13 at 15:38
  • @Joshua. The problem is that with chron the times are displayed in atomic values while I'm looking for a function that keeps the H:M:S format. – matteo Aug 07 '13 at 15:52
  • Show the output, because on my end, it looks like `00:05:00` – GSee Aug 07 '13 at 15:55
  • @matteo: the code in your question throws an error. If you have a data.frame and follow the instructions in the answer you linked to, it works just fine. – Joshua Ulrich Aug 07 '13 at 15:57

1 Answers1

-3

Use the POSIXt date/time data types. (see, for example, ?as.POSIXct). Here is an example with your data:

t <- c("00:05:00", "00:10:00", "00:15:00", "10:20:00", "10:25:00")
t2 <- as.POSIXct(t, format="%H:%M:%S")

diff(t2)

gives:

 > Time differences in mins
 > [1]   5   5 605   5
 > attr(,"tzone")

and:

difftime(t2[-1], t2[-length(t2)], units="hours")

gives:

 >Time differences in hours
 > [1]  0.08333333  0.08333333 10.08333333  0.08333333
 > attr(,"tzone")

Cheers, Eli

elzizi
  • 310
  • 1
  • 8
  • 1
    `diff`? `difftime`? why? – GSee Aug 07 '13 at 15:40
  • Just to show some functionality of time objects. – elzizi Aug 07 '13 at 15:41
  • 1
    This doesn't even come close to answering the question... – Joshua Ulrich Aug 07 '13 at 15:44
  • 1
    um ... the conversion of character stings to POSIX time objects is done with `as.POSIXct`. Was that not the question? – elzizi Aug 07 '13 at 15:48
  • @Eli, I already tried this function but the problem is that I need a new column where only the time is displayed. The POSIXct function adds the date values to the new column. – matteo Aug 07 '13 at 15:48
  • 3
    The question was how to convert character strings of times to a time class. `POSIXct` is a date-time class. It has no concept of _only_ hours, minutes, and seconds; they must be accompanied by a date. If no date is provided, it's assumed to be `Sys.Date()`, which is problematic if `Sys.Date()` happens to be a day on which daylight saving time changes in your timezone. – Joshua Ulrich Aug 07 '13 at 15:52
  • @matteo Can you be more clear? The time is displayed in the original character string. You can do `times(t)` from `chron` ... the display won't change, but you'll be able to manipulate them in a similar way to a POSIX. – elzizi Aug 07 '13 at 15:54
  • @Eli I need that R recognize the column as time values. I'm not able to plot values against those column if it is not formatted as time. Just like the function as.Date works for the dates. – matteo Aug 07 '13 at 15:58
  • `t <- c("00:05:00", "00:10:00", "00:15:00", "10:20:00", "10:25:00")`, followed by `times(t)` should work, no? – elzizi Aug 07 '13 at 15:59