[datatable-help] Integer date and time classes for use with data.table

Short, Tom TShort at epri.com
Mon Apr 12 23:08:41 CEST 2010


See enclosed for a draft of classes that implement dates and times with
integer storage. The IDate class is a simple wrapper around the Date
class that tries to keep an integer storage format. The ITime class, the
time of day, is stored as the number of seconds in a day.

Because IDate and ITime are stored as integers with ranges less than
100,000, data.table indexing and sorting is fast. Also included are
conversions to and from POSIXct and chron formats.

Comments and tests are welcome. 

Examples:

>     (t <- as.ITime("10:45:04"))
[1] "10:45:04"
>     (d <- as.IDate("2001-01-01"))
[1] "2001-01-01"
> 
>     datetime <- seq(as.POSIXct("2001-01-01"),
+                     as.POSIXct("2001-01-03"), by = "5 hour")
>     
>     (a <- data.table(IDateTime(datetime), a = rep(1:2, 5),
+                      key = "a,date,time"))
            date     time a
 [1,] 2001-01-01 00:00:00 1
 [2,] 2001-01-01 10:00:00 1
 [3,] 2001-01-02 06:00:00 1
 [4,] 2001-01-02 16:00:00 1
 [5,] 2001-01-02 20:00:00 1
 [6,] 2001-01-01 05:00:00 2
 [7,] 2001-01-01 15:00:00 2
 [8,] 2001-01-02 01:00:00 2
 [9,] 2001-01-02 11:00:00 2
[10,] 2001-01-03 21:00:00 2
> 
>     a[, mean(a), by = "date"]
           date  V1
[1,] 2001-01-01 1.5
[2,] 2001-01-02 1.4
[3,] 2001-01-03 2.0
> 
>     as.POSIXct(af$date, af$time) 
[1] "2001-01-01 00:00:00 EST" "2001-01-01 18:00:00 EST"
[3] "2001-01-02 12:00:00 EST" "2001-01-01 06:00:00 EST"
[5] "2001-01-02 00:00:00 EST" "2001-01-02 18:00:00 EST"
[7] "2001-01-01 12:00:00 EST" "2001-01-02 06:00:00 EST"
[9] "2001-01-03 00:00:00 EST"

- Tom

Tom Short
Electric Power Research Institute (EPRI)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: IDateTime.R
Type: application/octet-stream
Size: 5033 bytes
Desc: IDateTime.R
Url : http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20100412/97f03757/attachment.obj 


More information about the datatable-help mailing list