[R] calculating the number of days from dates

Martin Maechler maechler at stat.math.ethz.ch
Sat Dec 15 11:37:29 CET 2007


>>>>> "KK" == Knut Krueger <rh at family-krueger.com>
>>>>>     on Sat, 15 Dec 2007 08:39:02 +0100 writes:

    KK> Bob Green schrieb:
    >> 
    >> > dates <- read.csv("c:\\dates.csv",header=T)
    >> > dates
    >> v1         v2
    >> 1 12/12/1978 12/12/2005
    >> 2 23/01/1965 23/09/2001
    >> 3 24/12/2004 16/03/2007
    >> 4  3/03/2003  4/04/2004
    >> 5  8/11/2006  1/05/2007
    >> 
    >> > class(dates$v1)
    >> [1] "factor"
    >> > class(dates$v2)
    >> [1] "factor"
    >> 
    >> 
    KK> What about chron library:

it's  a  >> package <<  , not a library, please!

and as Henrique  has shown it's really not needed for the question.
There's the "Date" (S3) class, and even a "difftime" one
for time *differences*
See
	?as.Date
	?difftime
and also note the output of
     	methods(class = "Date")

Martin

    KK> dts <- dates(c("02/27/92", "02/27/92", "01/14/92",
    KK> "02/28/92", "02/01/92"))
    KK> dts
    KK> # [1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92
    KK> tms <- times(c("23:03:20", "22:29:56", "01:03:30",
    KK> "18:21:03", "16:56:26"))
    KK> tms
    KK> # [1] 23:03:20 22:29:56 01:03:30 18:21:03 16:56:26
    KK> x <- chron(dates = dts, times = tms)
    KK> x
    KK> # [1] (02/27/92 23:03:19) (02/27/92 22:29:56) (01/14/92 01:03:30)
    KK> # [4] (02/28/92 18:21:03) (02/01/92 16:56:26)

    KK> # We can add or subtract scalars (representing days) to dates or
    KK> # chron objects:
    KK> c(dts[1], dts[1] + 10)
    KK> # [1] 02/27/92 03/08/92
    KK> dts[1] - 31
    KK> # [1] 01/27/92


    KK> Knut

    KK> ______________________________________________
    KK> R-help at r-project.org mailing list
    KK> https://stat.ethz.ch/mailman/listinfo/r-help
    KK> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    KK> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list