I had previously posted a function for calculating age with two dates. This was for the whole number ‘age’ where we are assuming you don’t want someone to be recorded as ’18’ until their 18th birthday (so more than just YEAR – YEAR).
There was an error in the code when a certain combination was entered, so I’ve rewritten it, and this combination (with leap years) is now working correctly.
Code is available here: https://raw.github.com/nzcoops/r-code/master/age_function.R
require(RCurl)
source(textConnection(getURL(“https://raw.github.com/nzcoops/r-code/master/age_function.R”)))
dob <- as.Date(“2000-02-29”)
dov <- as.Date(“2004-02-28”)
age_years(dob, dov)
dob <- as.Date(“2000-02-29”)
dov <- as.Date(“2004-02-29”)
age_years(dob, dov)
dob <- as.Date(“2000-02-29”)
dov <- as.Date(“2004-03-01”)
age_years(dob, dov)
Returns
> dob <- as.Date(“2000-02-29”)
> dov <- as.Date(“2004-02-28”)
> age_years(dob, dov)
[1] 3
> dob <- as.Date(“2000-02-29”)
> dov <- as.Date(“2004-02-29”)
> age_years(dob, dov)
[1] 4
> dob <- as.Date(“2000-02-29”)
> dov <- as.Date(“2004-03-01”)
> age_years(dob, dov)
[1] 4
Pingback: Calculating Age | Matt's Stats n stuff
Why not use this:
trunc((dov – dob) / 365.242)
I’m not a fan of about dividing by an approximate number, also that code needs the as.numeric() around that to get rid of the ‘days’ unit. You can also miss anomalies in the data with that code.
All told, numerous different ways to do it, I was just surprised googling didn’t return many/any tidy options for it. Cheers.
I suggest just using:
age_in_years <- trunc((laterDate – earlierDate)/365.242199)
I ran your function against my simpler and more straight forward code on a sample of 83,873 people. The results were identical except your function produced one case of a negative age.
First line of the table results.
My method:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
67 122 172 158 237 285 334 420 479 503 582 628 651 730 797 840 1018 1181
Your function:
-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 66 122 172 158 237 285 334 420 476 506 582 628 651 730 797 840 1018
Thanks for the reply.
Can you post the two dates where it came up with the -ve value, otherwise it’s a bit tricky to work out.
Update:
The one negative age from your function was due to an error in the data, the person was NOT born yesterday! I had used Monday’s date as the later date, resulting in an age of negative one day. The simpler code didn’t trip up on it, when expressed as years of age, because the small negative decimal was truncated to zero, whereas your code expressed it as a whole negative year.
The bad data case:
gender laterDate dob
Female 2013-06-10 2013-06-11
Thanks for getting back to me.
I’m the first to say what I’ve written is pretty dirty, I just wanted something I knew would be a ‘catch all’, as while it works there’s something I’m not a fan of about dividing by an approximate number.
I guess the difference between your code and mine is that mine would help you identify that one patient that came in for a visit the day before they were born and yours wouldn’t. I guess you also need the as.numeric() around that to get rid of the days.
All told, numerous different ways to do it, I was just surprised googling didn’t return many/any tidy options for it. Cheers.
Pingback: Calculating ages | m's R Blog
For a different take on how to do this, here’s a function I wrote:
https://github.com/jknowles/eeptools/blob/master/R/age_calc.R
Some differences:
* Checks to see that each supplied date is of the right class.
* Returns age in units of years, months, or days.
* Uses difftime() from base which saves a lot of class conversions (and I believe takes care of leap year issues without any additional work).
Very nice. That (yours) is indeed the best way I’ve seen to do it.
It’s a shame google doesn’t find that when doing a search for an age function. I did want to add in the class checking etc, battled some proxy issues yesterday afternoon and wanted to get it up before leaving.
Glad to share. I wrote it for a similar reason– I was surprised there wasn’t more out there. The package I contributed to is intended to be a bunch of little “helper” functions for taking care of calculations and manipulations that are commonly needed with education data, which is the field I work in.
Maybe I’ll write something on my blog to give it some Google juice and make it easier for folks to find.
But then you’d trump me in the google rankings… haha
I’ve got a project coming up soon looking at educational data, do you mind if I touch base with you (have your email from the comment etc). Cheers.
Of course, contact me any time. I don’t have much Google Juice and I don’t submit to r-bloggers. You’ve got nothing to worry about.
Post is now here, FYI:
http://blog.jsonbecker.com/2013/06/calculating-age-in-r.html
A nice function, and much needed!
There’s also a function by Brian D. Ripley on R Help: http://r.789695.n4.nabble.com/Calculate-difference-between-dates-in-years-td835196.html. I have used that for years, but it does not deal with vectors of dates in a way your function does.