Calculating Age

Both ‘first’ and ‘second’ need to be date vectors. This is to achieve a calculation of age rounded DOWN in the same way that we say our age (as in we say 17, right up until the day we turn 18). round(x,0) will round up, floor(x) will also cause issues, if the two dates are the same day of the same month but on different years.

EDIT: This had an error and has been superseded by this post.

About these ads

5 thoughts on “Calculating Age

  1. This unfortunately does not work if birth day is 29 Feb and “second” is any date in non-leap-year.

    > age_years(as.Date(“2000-02-29″), as.Date(“2013-02-27″))
    Error in charToDate(x) : character string is not in a standard unambiguous format

  2. Pingback: Updated age calculation function | Matt's Stats n stuff

  3. Maybe you can clarify for me the utility of doing this. If someone is 17.999 years old he or she is still under 18. Why round ages at all? Is there some compelling reason to represent age as integers? Typically I’m asked to put ages into ranges and have no need to round up or down.

    • There’s a few reasons you might like to do this.
      – If you have a requirement of people to be exactly ’18’ or ’25’ before entering something, then you need their age spot on as you can’t have them creeping into the category a day early because of dud rounding.
      – If you are working on data with 1,000’s of children say and have tight age categories 10-12, 13-15 etc, you don’t want lot’s of children getting assigned to the wrong group because of rounding issues that can occur with cruder methods.

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