Create a Chronological Object

DESCRIPTION:

Create objects representing dates and times.

USAGE:

chron(dates, times, format=c(dates="m/d/y", times="h:m:s"),
      out.format, origin, century=1930)

REQUIRED ARGUMENTS:

dates
character or numeric vector specifying dates. If character, dates are assumed to be in one of the date formats below; if numeric, dates are assumed to be Julian dates, i.e., number of days since origin.
times
optional character or numeric vector specifying times of day. If character, times are assumed to be in one of the time formats below; if numeric, times are assumed to be fractions of a day.
format
vector or list specifying the input format of the input. The format can be either strings specifying one of the recognized formats below or a list of user-supplied functions to convert dates from character into Julian dates and vice versa.

The dates format can be any permutation of the characters "d", "m", or "y" delimited by a separator (possibly null), e.g., "m/d/y", "d-m-y", "ymd", are all valid. The format can also be permutations of the words "day" "month" and "year", which produces the month name, e.g., "month day year" produces "April 20 1992", "day mon year" produces "20 Apr 1992".

The times format can be any permutation of "h", "m", and "s" separated by any one non-special character. The default is "h:m:s".

out.format
vector or list specifying date and time format for printing and output. Default is same as format.
origin
a vector specifying the date with respect to which Julian dates are computed. Default is c(month=1, day=1, year=1970); you may set the option chron.origin to specify your own default, e.g., options(chron.origin=c(month=1, day=1, year=1990)).
century
starting year for 100-year period that 2-digit dates represent. The default value of 1930 means that 2-digit dates run from 1930 to 2029, so that e.g. "1/1/45" is in 1945 and "1/1/22" is 2022.

VALUE:

An object of class times if only times were specified, dates if only dates, or chron if both dates= and times= were supplied. All these inherit from class times.

These objects represent dates and times of day, and allow the following arithmetic and summaries: subtraction d1-d2, constant addition d1+constants, all logical comparisons, summaries min(), max() , and range() (which drop NAs by default); constants specify days (fractions are converted to time-of-day, e.g., 2.5 represents 2 days and 12 hours). Operations such as sorting, differencing, etc., are automatically handled.

There are methods for as.character , as.numeric , cut , is.na , print , summary , plot , lines , lag , and the usual subsetting functions [, [<- . The functions days, months , quarters , years , weeks weekdays, hours , minutes , and seconds take any chron object as input and extract the corresponding time interval. The function cut is used to create ordered factors from chron objects. Chronological objects may be used with the modeling software.

The current implementation of chron objects does not handle time zones nor daylight savings time.

SEE ALSO:

, , .

EXAMPLES:

mr.date <- chron(c("09/28/89", "02/09/90", "09/15/88", "08/30/91"))
mr.date
# The following should be returned:
# [1] 09/28/89 02/09/90 09/15/88 08/30/91
range(mr.date)
# [1] 09/15/88 08/30/91
mr.date[mr.date > "01/01/90"]
# [1] 02/09/90 08/30/91

# Need to create objects dts and tms:
lathe.date <- chron(dts, tms, format=c("ymd", "h:m:s") )
lathe.date
# [1] (09/28/90 23:12:55) (10/22/90 10:34:02) (11/06/90 08:30:00)
# [4] (11/19/90 01:15:00) (11/28/90 20:00:00)
lathe.date[1] + 2.5    # two and a half days after lathe start
# [1] (10/01/90 11:12:55)
lathe.date[2] - lathe.date[1]  # time between events
# Time in days:
# [1] 24.473
summary(mr.date)