Hello all, Can somebody point me to references or provide some code on dealing with this date issue. Basically, I have two vectors of values that represent dates. I want to convert these values into a date format and subtract the differences to show elapsed time in days. More specifically, here is the example: Date1 Date2 032398 061585 032398 061585 111694 101994 111694 101994 062695 021595 051898 111597 072495 040195 072495 040195 The dates are in the mmddyy format, but when I attempt to format these in R with the function, date.mmddyy(Date1), I get very odd results. Any help on this matter would be greatly appreciated! Thanks in advance, Brian [[alternative HTML version deleted]]
Try this:> x <- 'Date1 Date2+ 032398 061585 + 032398 061585 + 111694 101994 + 111694 101994 + 062695 021595 + 051898 111597 + 072495 040195 + 072495 040195'> data.in <- read.table(textConnection(x), header=TRUE,colClasses=c('character', 'character'))> data.in$Date1 <- as.POSIXct(strptime(data.in$Date1, "%m%d%y")) > data.in$Date2 <- as.POSIXct(strptime(data.in$Date2, "%m%d%y")) > data.in$diff <- difftime(data.in$Date1, data.in$Date2, units='days') > > > data.inDate1 Date2 diff 1 1998-03-23 1985-06-15 4664 2 1998-03-23 1985-06-15 4664 3 1994-11-16 1994-10-19 28 4 1994-11-16 1994-10-19 28 5 1995-06-26 1995-02-15 131 6 1998-05-18 1997-11-15 184 7 1995-07-24 1995-04-01 114 8 1995-07-24 1995-04-01 114>On 12/27/06, Brian Edward <perronbe@gmail.com> wrote:> > Hello all, > > Can somebody point me to references or provide some code on dealing with > this date issue. Basically, I have two vectors of values that represent > dates. I want to convert these values into a date format and subtract the > differences to show elapsed time in days. More specifically, here is the > example: > > Date1 Date2 > 032398 061585 > 032398 061585 > 111694 101994 > 111694 101994 > 062695 021595 > 051898 111597 > 072495 040195 > 072495 040195 > > The dates are in the mmddyy format, but when I attempt to format these in > R > with the function, date.mmddyy(Date1), I get very odd results. Any help > on > this matter would be greatly appreciated! > > Thanks in advance, > Brian > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
See the documentation for as.Date Something like (untested): as.Date(Date1, '"%m%d%y") - as.Date(Date2,"%m%d%y") That's assuming Date1 and Date2 have already been loaded into R, and are character vectors. They would have to be character vectors in order to display the leading zero, as in 032398. -Don At 10:18 PM -0600 12/27/06, Brian Edward wrote:>Hello all, > >Can somebody point me to references or provide some code on dealing with >this date issue. Basically, I have two vectors of values that represent >dates. I want to convert these values into a date format and subtract the >differences to show elapsed time in days. More specifically, here is the >example: > >Date1 Date2 >032398 061585 >032398 061585 >111694 101994 >111694 101994 >062695 021595 >051898 111597 >072495 040195 >072495 040195 > >The dates are in the mmddyy format, but when I attempt to format these in R >with the function, date.mmddyy(Date1), I get very odd results. Any help on >this matter would be greatly appreciated! > >Thanks in advance, >Brian > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch 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.-- --------------------------------- Don MacQueen Lawrence Livermore National Laboratory Livermore, CA, USA
Thanks for the help in addressing my query regarding the formatting of dates. All the suggestions were very helpful and I seem to be on track with solving the problem. Don, you are exactly right in your posting -- that is, the issue of the leading zero. And that is where my problem is. When I read the CSV file into R, the leading zero is dropped. Here is sample code for reading the file. Can you give me a suggestion on how to keep the leading zero? That seems to be my last remaining problem. DATES <- read.csv("C:/Data/dates.csv", header=TRUE, sep=",", dec=".", strip.white=TRUE) Thanks in advance. -Brian "They would have to be character vectors in> order to display the leading zero, as in 032398." > >[[alternative HTML version deleted]]