Updated age calculation function

June 12, 2013
By

[This article was first published on Matt's Stats n stuff » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

 

 

 

To leave a comment for the author, please follow the link and comment on their blog: Matt's Stats n stuff » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)