Re: [R] calculating the number of days from dates

From: Knut Krueger <rh_at_family-krueger.com>
Date: Sat, 15 Dec 2007 08:39:02 +0100

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"
>
>
What about chron library:

dts <- dates(c("02/27/92", "02/27/92", "01/14/92",

               "02/28/92", "02/01/92"))
dts
# [1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92 tms <- times(c("23:03:20", "22:29:56", "01:03:30",

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

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

Knut



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 15 Dec 2007 - 07:44:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 15 Dec 2007 - 12:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.