s-news
[Top] [All Lists]

Re: finding differences in dates

To: "Data Analytics Corp." <dataanalytics@earthlink.net>
Subject: Re: finding differences in dates
From: David L Lorenz <lorenz@usgs.gov>
Date: Fri, 5 Oct 2007 10:25:04 -0500
Cc: s-news@lists.biostat.wustl.edu, s-news-owner@lists.biostat.wustl.edu
In-reply-to: <47064498.20009@earthlink.net>

Walt,
  There was a thread on this topic back in 2004. As you note, the computation of difference in years is complicated by the differing number of days in years. A day is a fixed period of time, so it makes sense to report the values of subtracting timeDate data in days.
  A function to compute the age of an individual was provided in that thread. I think that Tony Plate wrote it. Here it is.

cal.years <- function(from, to) {
     caly <- numeric(length(from))
     for (i in seq(along=from)) {
         ay.rnd <- round(as.numeric(to[i] - from[i]) / 365.25)
         target <- from[i]+timeRelative(by="years", k.by=ay.rnd)
         targetp1 <- from[i]+timeRelative(by="years", k.by=ay.rnd+1)
         caly[i] <- if (to[i]<target) ay.rnd-1 else if (to[i]>=targetp1) ay.rnd+1 else ay.rnd
     }
     return(caly)
}

  Note that this function will give a value of 0 for the time span from 03/01/2006 to 02/28/2007 because 1 year from 03/01/2006 is 03/01/2007. You could simply add one to the result to get the answer you want, or modify the code for your purpose. I have not verified that the code works, but it did work for the example that was used in the posting.
Dave


"Data Analytics Corp." <dataanalytics@earthlink.net>
Sent by: s-news-owner@lists.biostat.wustl.edu

10/05/2007 09:05 AM

To
s-news@lists.biostat.wustl.edu
cc
Subject
[S] finding differences in dates





Good morning,

I have a data frame with an anniversary and expiration date for people
belonging to a club.  The dates are in mm/dd/yyyy format.  I want to
find the number of years from anniversary to expiration.  I used
timeDate(expiration) - timeDate(anniversary) but it looks like I get the
number of days.  Short of dividing by 365 (which won't allow for leap
years),  I want a function to find the number of years, preferably to
the next largest year (so if one anniversary is 03/01/2006 and
expiration is 02/28/2007, then I want years = 1).  How do I do this?  
Any help is appreciated.

Thanks,

Walt Paczkowski
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>