1

I have a chron object like this:

 t <- as.chron("06/01/13 01:00:00", "%m/%d/%y %H:%M:%S") 

and I want to convert it to seconds from origin, where the origin is:

 or <- "06/01/13 00:00:00"

so what I want is that the object t become:

 t <- 3600

Thank you very much Giulia

Giulia
  • 279
  • 1
  • 4
  • 14

1 Answers1

1

Just use difftime():

R> library(chron)
R>  t <- as.chron("06/01/13 01:00:00", "%m/%d/%y %H:%M:%S") 
R> or <- as.chron("06/01/13 00:00:00", "%m/%d/%y %H:%M:%S") 
R> difftime(t, or)
Time difference of 1 hours
R> 

or in the units you want:

R> difftime(t, or, unit="secs")
Time difference of 3600 secs
R> as.numeric(difftime(t, or, unit="secs"))
[1] 3600
R> 

As an aside, you probably want to use POSIXct instead of chron which difftime() converts to for you anyway. See help(DateTimeClasses).

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725