Daniel Brewer wrote:
> 
> I would like to be able to calculate the age of someone at a particular
> date.  Both dates are date objects.  Here is what I have come up with:
> 
> floor(as.numeric(sampleInfo$Date.of.DIAGNOSIS-
> sampleInfo$Date.of.birth)/365.25)
> 
> Is this the best approach?
>
No - leap years and such. You know that there are _not_
365.25 days in one year, don't you?

floor(as.numeric(as.Date("2100-02-28") - as.Date("1900-02-28"))/365.25)
# 199, should be 200

A less extreme counter-example:

floor(as.numeric(as.Date("2008-02-28") - as.Date("2007-02-28"))/365.25)
# 0, should be 1

Alberto Monteiro (purely destructive - sorry)

______________________________________________
R-help@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.

Reply via email to