[R] Calculate difference between dates in years
Alberto Monteiro
albmont at centroin.com.br
Mon Sep 24 13:18:18 CEST 2007
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)
More information about the R-help
mailing list