IDateTime.RdDate and time classes with integer storage for fast sorting and grouping. Still experimental!
as.IDate(x, ...) # S3 method for default as.IDate(x, ..., tz = attr(x, "tzone", exact=TRUE)) # S3 method for Date as.IDate(x, ...) # S3 method for IDate as.Date(x, ...) # S3 method for IDate as.POSIXct(x, tz = "UTC", time = 0, ...) # S3 method for IDate round(x, digits = c("weeks", "months", "quarters","years"), ...) as.ITime(x, ...) # S3 method for default as.ITime(x, ...) # S3 method for POSIXlt as.ITime(x, ms = 'truncate', ...) # S3 method for ITime as.POSIXct(x, tz = "UTC", date = Sys.Date(), ...) # S3 method for ITime as.character(x, ...) # S3 method for ITime format(x, ...) IDateTime(x, ...) # S3 method for default IDateTime(x, ...) second(x) minute(x) hour(x) yday(x) wday(x) mday(x) week(x) isoweek(x) month(x) quarter(x) year(x)
| x | an object |
|---|---|
| ... | arguments to be passed to or from other methods. For
|
| tz | time zone (see |
| date | date object convertible with |
| time | time-of-day object convertible with |
| digits | really |
| ms | For |
IDate is a date class derived from Date. It has the same
internal representation as the Date class, except the storage
mode is integer. IDate is a relatively simple wrapper, and it
should work in almost all situations as a replacement for Date.
Functions that use Date objects generally work for
IDate objects. This package provides specific methods for
IDate objects for mean, cut, seq, c,
rep, and split to return an IDate object.
ITime is a time-of-day class stored as the integer number of
seconds in the day. as.ITime does not allow days longer than 24
hours. Because ITime is stored in seconds, you can add it to a
POSIXct object, but you should not add it to a Date
object.
Conversions to and from Date and POSIXct formats are provided.
ITime does not account for time zones. When converting
ITime and IDate to POSIXct with as.POSIXct, a time
zone may be specified.
Inputs like '2018-05-15 12:34:56.789' are ambiguous from the perspective of an ITime object -- the method of coercion of the 789 milliseconds is controlled by the ms argument to relevant methods. The default behavior (ms = 'truncate') is to use as.integer, which has the effect of truncating anything after the decimal. Alternatives are to round to the nearest integer (ms = 'nearest') or to round up (ms = 'ceil').
In as.POSIXct methods for ITime and IDate, the
second argument is required to be tz based on the generic
template, but to make converting easier, the second argument is
interpreted as a date instead of a time zone if it is of type
IDate or ITime. Therefore, you can use either of the
following: as.POSIXct(time, date) or as.POSIXct(date,
time).
IDateTime takes a date-time input and returns a data table with
columns date and time.
Using integer storage allows dates and/or times to be used as data table
keys. With positive integers with a range less than 100,000, grouping
and sorting is fast because radix sorting can be used (see
sort.list).
Several convenience functions like hour and quarter are
provided to group or extract by hour, month, and other date-time
intervals. as.POSIXlt is also useful. For example,
as.POSIXlt(x)$mon is the integer month. The R base convenience
functions weekdays, months, and quarters can also
be used, but these return character values, so they must be converted to
factors for use with data.table. isoweek is ISO 8601-consistent.
The round method for IDate's is useful for grouping and plotting. It can
round to weeks, months, quarters, and years.
For as.IDate, a class of IDate and Date with the
date stored as the number of days since some origin.
For as.ITime, a class of ITime
stored as the number of seconds in the day.
For IDateTime, a data table with columns idate and
itime in IDate and ITime format.
second, minute, hour, yday, wday,
mday, week, month, quarter,
and year return integer values
for second, minute, hour, day of year, day of week,
day of month, week, month, quarter, and year, respectively.
These values are all taken directly from the POSIXlt representation of x, with the notable difference that while yday, wday, and mon are all 0-based, here they are 1-based.
G. Grothendieck and T. Petzoldt, ``Date and Time Classes in R,'' R News, vol. 4, no. 1, June 2004.
H. Wickham, http://gist.github.com/10238.
ISO 8601, http://www.iso.org/iso/home/standards/iso8601.htm
# create IDate: (d <- as.IDate("2001-01-01"))#> [1] "2001-01-01"#> [1] TRUE# create ITime: (t <- as.ITime("10:45"))#> [1] "10:45:00"#> [1] TRUE(t <- as.ITime("10:45:04"))#> [1] "10:45:04"(t <- as.ITime("10:45:04", format = "%H:%M:%S"))#> [1] "10:45:04"#> [1] "2001-01-01 10:45:00 UTC"datetime <- seq(as.POSIXct("2001-01-01"), as.POSIXct("2001-01-03"), by = "5 hour") (af <- data.table(IDateTime(datetime), a = rep(1:2, 5), key = "a,idate,itime"))#> idate itime a #> 1: 2001-01-01 00:00:00 1 #> 2: 2001-01-01 10:00:00 1 #> 3: 2001-01-01 20:00:00 1 #> 4: 2001-01-02 06:00:00 1 #> 5: 2001-01-02 16: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-02 21:00:00 2#> itime V1 #> 1: 00:00:00 1 #> 2: 10:00:00 1 #> 3: 20:00:00 1 #> 4: 06:00:00 1 #> 5: 16:00:00 1 #> 6: 05:00:00 2 #> 7: 15:00:00 2 #> 8: 01:00:00 2 #> 9: 11:00:00 2 #> 10: 21:00:00 2#> hour V1 #> 1: 0 1 #> 2: 10 1 #> 3: 20 1 #> 4: 6 1 #> 5: 16 1 #> 6: 5 2 #> 7: 15 2 #> 8: 1 2 #> 9: 11 2 #> 10: 21 2#> wday V1 #> 1: Monday 1.4 #> 2: Tuesday 1.6#> wday V1 #> 1: 2 1.4 #> 2: 3 1.6#> [1] "2001-01-01 UTC" "2001-01-01 UTC" "2001-01-01 UTC" "2001-01-02 UTC" #> [5] "2001-01-02 UTC" "2001-01-01 UTC" "2001-01-01 UTC" "2001-01-02 UTC" #> [9] "2001-01-02 UTC" "2001-01-02 UTC"#> [1] "2001-01-01 00:00:00 UTC" "2001-01-01 10:00:00 UTC" #> [3] "2001-01-01 20:00:00 UTC" "2001-01-02 06:00:00 UTC" #> [5] "2001-01-02 16:00:00 UTC" "2001-01-01 05:00:00 UTC" #> [7] "2001-01-01 15:00:00 UTC" "2001-01-02 01:00:00 UTC" #> [9] "2001-01-02 11:00:00 UTC" "2001-01-02 21:00:00 UTC"#> [1] "2001-01-01 00:00:00 UTC" "2001-01-01 10:00:00 UTC" #> [3] "2001-01-01 20:00:00 UTC" "2001-01-02 06:00:00 UTC" #> [5] "2001-01-02 16:00:00 UTC" "2001-01-01 05:00:00 UTC" #> [7] "2001-01-01 15:00:00 UTC" "2001-01-02 01:00:00 UTC" #> [9] "2001-01-02 11:00:00 UTC" "2001-01-02 21:00:00 UTC"#> [1] "2001-01-01 00:00:00 GMT" "2001-01-01 10:00:00 GMT" #> [3] "2001-01-01 20:00:00 GMT" "2001-01-02 06:00:00 GMT" #> [5] "2001-01-02 16:00:00 GMT" "2001-01-01 05:00:00 GMT" #> [7] "2001-01-01 15:00:00 GMT" "2001-01-02 01:00:00 GMT" #> [9] "2001-01-02 11:00:00 GMT" "2001-01-02 21:00:00 GMT"#> [1] "2001-01-01 00:00:00 UTC" "2001-01-01 10:00:00 UTC" #> [3] "2001-01-01 20:00:00 UTC" "2001-01-02 06:00:00 UTC" #> [5] "2001-01-02 16:00:00 UTC" "2001-01-01 05:00:00 UTC" #> [7] "2001-01-01 15:00:00 UTC" "2001-01-02 01:00:00 UTC" #> [9] "2001-01-02 11:00:00 UTC" "2001-01-02 21:00:00 UTC"#> [1] "2019-12-31 00:00:00 UTC" "2019-12-31 10:00:00 UTC" #> [3] "2019-12-31 20:00:00 UTC" "2019-12-31 06:00:00 UTC" #> [5] "2019-12-31 16:00:00 UTC" "2019-12-31 05:00:00 UTC" #> [7] "2019-12-31 15:00:00 UTC" "2019-12-31 01:00:00 UTC" #> [9] "2019-12-31 11:00:00 UTC" "2019-12-31 21:00:00 UTC"#> [1] "2001-01-01" "2001-01-22" "2001-02-12" "2001-03-05" "2001-03-26" #> [6] "2001-04-16" "2001-05-07" "2001-05-28" "2001-06-18" "2001-07-09" #> [11] "2001-07-30"#> [1] "2001-01-01" "2001-01-01" "2001-02-01" "2001-03-01" "2001-03-01" #> [6] "2001-04-01" "2001-05-01" "2001-05-01" "2001-06-01" "2001-07-01" #> [11] "2001-07-01"