Updated age calculation function

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

 

 

 

About these ads

14 thoughts on “Updated age calculation function

  1. Pingback: Calculating Age | Matt's Stats n stuff

    • 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.

  2. 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

  3. 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.

  4. Pingback: Calculating ages | m's R Blog

    • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s