You want a (standard) POSIXt type from the R base, which can be used in "compact form", like POSIXct (which is essentially double representing fractional seconds from the era) or as a long form in POSIXlt (which contains subelements). The most interesting thing is that arithmetic, etc. Defined on this - see help(DateTimeClasses)
Quick example:
R> now <- Sys.time() R> now [1] "2009-12-25 18:39:11 CST" R> as.numeric(now) [1] 1.262e+09 R> now + 10 # adds 10 seconds [1] "2009-12-25 18:39:21 CST" R> as.POSIXlt(now) [1] "2009-12-25 18:39:11 CST" R> str(as.POSIXlt(now)) POSIXlt[1:9], format: "2009-12-25 18:39:11" R> unclass(as.POSIXlt(now)) $sec [1] 11.79 $min [1] 39 $hour [1] 18 $mday [1] 25 $mon [1] 11 $year [1] 109 $wday [1] 5 $yday [1] 358 $isdst [1] 0 attr(,"tzone") [1] "America/Chicago" "CST" "CDT" R>
Regarding reading them, see help(strptime)
As for the difference, itβs also easy:
R> Jan1 <- strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S") R> difftime(now, Jan1, unit="week") Time difference of 51.25 weeks R>
Finally, zoo package is an extremely versatile and well-documented container for the matrix with corresponding date and time indices.
Dirk Eddelbuettel Dec 26 '09 at 0:45 2009-12-26 00:45
source share