[R] calculating the number of days from dates

From: Bob Green <bgreen_at_dyson.brisnet.org.au>
Date: Fri, 14 Dec 2007 22:05:19 +1000

Hello,

I gather variants of this question have been asked previously. I have done some reading but only became more confused, as I suspect what I am trying to do is more basic than other applications.

The following code readily calculates the difference in days between two dates:

  newdays <- ISOdate(2005, 5,12) - ISOdate(2006, 12, 22)

However, I wanted to be able to deduct the dates in one variable from the dates in another variable, resulting in a new variable - e.g the difference in days between the two dates. Below is a sample of my data. My questions:

  1. I tried changing the data to dates via as.Date. Is this necessary or do I need to alter the date format itself, e.g to 12/12/78 or some other format?
  2. I gather there are various packages as well as date formats.What is the most straight forward approach to calculate the difference between two dates, as below.

> 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"

> dates <- read.csv("c:\\dates.csv",header=T,
as.Date(as.character(dates) "%d/%m/%Y")) Error: syntax error, unexpected STR_CONST, expecting ',' in "dates <- read.csv("c:\\dates.csv",header=T, as.Date(as.character(dates) "%d/%m/%Y""
>

Any assistance is much appreciated,

Bob



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 Fri 14 Dec 2007 - 12:08:30 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 - 08: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.