[R] date to age

William Briggs wib2004 at med.cornell.edu
Mon Jun 30 20:08:32 CEST 2003


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?




More information about the R-help mailing list