Skip to content

date to age

2 messages · William Briggs, Brian Ripley

#
I have files which have columns of data that look like this:

DOB
9/27/1964
...

That is, dates in month/day/year format.  When variables like DOB are read
in, they are converted to factors.

So, I wrote this to convert from date to age in years:

age<-function(x)
{as.numeric(Sys.time()-strptime(as.character(x),format="%m/%d/%Y"))/365.25}

This isn't very precise or pretty, but it works.

Does anybody have something better?
#
On Mon, 30 Jun 2003, William Briggs wrote:

            
Why?  You can avoid that, of course.
Convert to POSIXlt and compare differences in years, months, days etc.
Write an extension to difftime() to handle years and contribute it?