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

Rob Forler rforler at uchicago.edu
Tue Apr 13 00:05:29 CEST 2010


this looks quite beneficial.

I find myself doing the following a lot

dt$Date= as.integer(format(dt$Date, "%Y%m%d"))

So this should take care of this problem.

I'll see where I can use this and try to test it out,
Rob

On Mon, Apr 12, 2010 at 4:08 PM, Short, Tom <TShort at epri.com> wrote:

> 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)
>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20100412/7e25c40b/attachment.htm 


More information about the datatable-help mailing list