ITime) with integer storage
for fast sorting and grouping.
IDate inherits from the base class
Date; the main
difference is that the latter uses double storage, allowing e.g. for
fractional dates at the cost of storage & sorting inefficiency.
IDate, if sub-day granularity is needed, use a second
IDateTime() facilitates building such
Lastly, there are date-time helpers for extracting parts of dates as
integers, for example the year (
month()), or day in the month (
mday()); see Usage and Exampels.
as.IDate(x, ...) ## Default S3 method: as.IDate(x, ..., tz = attr(x, "tzone", exact=TRUE)) ## S3 method for class 'Date' as.IDate(x, ...) ## S3 method for class 'IDate' as.Date(x, ...) ## S3 method for class 'IDate' as.POSIXct(x, tz = "UTC", time = 0, ...) ## S3 method for class 'IDate' round(x, digits = c("weeks", "months", "quarters","years"), ...) as.ITime(x, ...) ## Default S3 method: as.ITime(x, ...) ## S3 method for class 'POSIXlt' as.ITime(x, ms = 'truncate', ...) ## S3 method for class 'ITime' round(x, digits = c("hours", "minutes"), ...) ## S3 method for class 'ITime' trunc(x, units = c("hours", "minutes"), ...) ## S3 method for class 'ITime' as.POSIXct(x, tz = "UTC", date = Sys.Date(), ...) ## S3 method for class 'ITime' as.character(x, ...) ## S3 method for class 'ITime' format(x, ...) IDateTime(x, ...) ## Default S3 method: IDateTime(x, ...) second(x) minute(x) hour(x) yday(x) wday(x) mday(x) week(x) isoweek(x) month(x) quarter(x) year(x) yearmon(x) yearqtr(x)
arguments to be passed to or from other methods. For
time zone (see
date object convertible with
time-of-day object convertible with
one of the units listed for truncating. May be abbreviated.
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. The main limitations of integer storage are (1) fractional
dates are not supported (use
IDateTime() instead) and (2) the
range of supported dates is bounded by
dates away from January 1, 1970 (a rather impractical limitation as
these dates are roughly 6 million years in the future/past, but
consider this your caveat).
Functions that use
Date objects generally work for
IDate objects. This package provides specific methods for
IDate objects for
split to return an
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
ITime is stored in seconds, you can add it to a
POSIXct object, but you should not add it to a
We also provide S3 methods to convert to and from
ITime is time zone-agnostic. When converting
IDate to POSIXct with
as.POSIXct, a time zone may be specified.
'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').
as.POSIXct methods for
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
ITime. Therefore, you can use either of the
as.POSIXct(time, date) or
IDateTime takes a date-time input and returns a data table with
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
Several convenience functions like
provided to group or extract by hour, month, and other date-time
as.POSIXlt is also useful. For example,
as.POSIXlt(x)$mon is the integer month. The R base convenience
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.
round method for IDate's is useful for grouping and plotting.
It can round to weeks, months, quarters, and years. Similarly, the
trunc methods for ITime's are useful for grouping and plotting.
They can round or truncate to hours and minutes.
Note for ITime's with 30 seconds, rounding is inconsistent due to rounding off a 5.
See 'Details' in
round for more information.
as.IDate, a class of
Date with the
date stored as the number of days since some origin.
as.ITime, a class of
stored as the number of seconds in the day.
IDateTime, a data table with columns
year return integer values
for second, minute, hour, day of year, day of week,
day of month, week, month, quarter, and year, respectively.
yearqtr return double values representing
respectively 'year + (month-1) / 12' and 'year + (quarter-1) / 4'.
hour are taken directly from
All other values are computed from the underlying integer representation
and comparable with the values of their
x, with the notable difference that while
mon are all 0-based, here they are 1-based.
Tom Short, firstname.lastname@example.org
G. Grothendieck and T. Petzoldt, “Date and Time Classes in R,” R News, vol. 4, no. 1, June 2004.
H. Wickham, https://gist.github.com/10238.
ISO 8601, https://www.iso.org/iso/home/standards/iso8601.htm
# create IDate: (d <- as.IDate("2001-01-01")) # S4 coercion also works identical(as.IDate("2001-01-01"), as("2001-01-01", "IDate")) # create ITime: (t <- as.ITime("10:45")) # S4 coercion also works identical(as.ITime("10:45"), as("10:45", "ITime")) (t <- as.ITime("10:45:04")) (t <- as.ITime("10:45:04", format = "%H:%M:%S")) as.POSIXct("2001-01-01") + as.ITime("10:45") 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")) af[, mean(a), by = "itime"] af[, mean(a), by = list(hour = hour(itime))] af[, mean(a), by = list(wday = factor(weekdays(idate)))] af[, mean(a), by = list(wday = wday(idate))] as.POSIXct(af$idate) as.POSIXct(af$idate, time = af$itime) as.POSIXct(af$idate, af$itime) as.POSIXct(af$idate, time = af$itime, tz = "GMT") as.POSIXct(af$itime, af$idate) as.POSIXct(af$itime) # uses today's date (seqdates <- seq(as.IDate("2001-01-01"), as.IDate("2001-08-03"), by = "3 weeks")) round(seqdates, "months") (seqtimes <- seq(as.ITime("07:00"), as.ITime("08:00"), by = 20)) round(seqtimes, "hours") trunc(seqtimes, "hours")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.