Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with time-periods such as 5 minutes and 30 seconds in R

Tags:

posix

r

zoo

Is there a good way to deal with time periods such as 05:30 (5 minutes, 30 seconds) in R?

Alternatively what's the fastest way to convert it into an integer with just seconds?

I can only convert to dates and can't really find a data type for time.

I'm using R with zoo.

Thanks a lot !


Seconds was the best way to deal with this. I adapted Shane's code below to my purposes, here's the result.

# time - time in the format of dd hh:mm:ss
#       (That's the format used in cvs export from Alcatel CCS reports)
#
time.to.seconds <- function(time) {

   t <- strsplit(as.character(time), " |:")[[1]]
   seconds <- NaN

   if (length(t) == 1 )
      seconds <- as.numeric(t[1])
   else if (length(t) == 2)
      seconds <- as.numeric(t[1]) * 60 + as.numeric(t[2])
   else if (length(t) == 3)
      seconds <- (as.numeric(t[1]) * 60 * 60 
          + as.numeric(t[2]) * 60 + as.numeric(t[3]))   
   else if (length(t) == 4)
      seconds <- (as.numeric(t[1]) * 24 * 60 * 60 +
         as.numeric(t[2]) * 60 * 60  + as.numeric(t[3]) * 60 +
         as.numeric(t[4]))

   return(seconds)
}
like image 349
tovare Avatar asked Dec 14 '22 02:12

tovare


2 Answers

Yes, while there is no 'time' type, you can use an offset time:

R> now <- Sys.time()
R> now
[1] "2009-09-07 08:40:32 CDT"
R> class(now)
[1] "POSIXt"  "POSIXct"
R> later <- now + 5*60
R> later
[1] "2009-09-07 08:45:32 CDT"
R> class(later)
[1] "POSIXt"  "POSIXct"
R> tdelta <- difftime(later, now)
R> tdelta
Time difference of 5 mins
R> class(tdelta)
[1] "difftime"
R> 

When you use the zoo package, you are using standard POSIXt types for your times indices. Both zoo and the newer and also highly-recommended xts package can use POSIXt, and especially the compact POSIXct type, for indexing.

The xts package has a lot more indexing functionality, and Jeff recently added parsing of intervals according to the ISO8601-2004(e) specification and gives these references for ISO8601 and a FAQ for widely used standars for date and time formats. To use this xts version, you may need to switch the the xts development snapshot on r-forge

[Edit:] Also, regarding the question on 'conversion': this is easy once you have objects of class POSIXt / POSIXct as as.numeric() will convert POSIXct to (fractional) seconds since the epoch. R goes further than the POSIX standard and uses a double here, so you get millisecond precision:

R> options("digits.secs"=6)   ## needed so that fractional seconds are printed
R> now <- Sys.time(); difftime(Sys.time(), now)
Time difference of 0.000149 secs

and

R> print(as.numeric(now), digits=15)   ## print with digits for sub-second time
[1] 1252374404.22975
R> 
like image 183
Dirk Eddelbuettel Avatar answered Dec 21 '22 22:12

Dirk Eddelbuettel


As Dirk points out, there is an object called "difftime", but it can't be added/subtracted.

> as.difftime(5, units="mins")
Time difference of 5 mins

> d <- seq(from=as.POSIXct("2003-01-01"), to=as.POSIXct("2003-01-04"), by="days")
> d
[1] "2003-01-01 GMT" "2003-01-02 GMT" "2003-01-03 GMT" "2003-01-04 GMT"

> d + as.difftime(5, units="mins")
[1] "2003-01-01 00:00:05 GMT" "2003-01-02 00:00:05 GMT"
[3] "2003-01-03 00:00:05 GMT" "2003-01-04 00:00:05 GMT"
Warning message:
Incompatible methods ("+.POSIXt", "Ops.difftime") for "+" 

Seems you can now do this:

  > as.difftime(5, units='mins')
    Time difference of 5 mins
    > d <- seq(from=as.POSIXct("2003-01-01"), to=as.POSIXct("2003-01-04"), by="days")
    > d 
    [1] "2003-01-01 GMT" "2003-01-02 GMT" "2003-01-03 GMT" "2003-01-04 GMT"
    > d + as.difftime(5, unit='mins')
    [1] "2003-01-01 00:05:00 GMT" "2003-01-02 00:05:00 GMT"
    [3] "2003-01-03 00:05:00 GMT" "2003-01-04 00:05:00 GMT"
    > d + as.difftime(5, unit='secs')
    [1] "2003-01-01 00:00:05 GMT" "2003-01-02 00:00:05 GMT"
    [3] "2003-01-03 00:00:05 GMT" "2003-01-04 00:00:05 GMT"
   >

This is with the recently released R 2.15.0

like image 41
Shane Avatar answered Dec 21 '22 22:12

Shane