Updated age calculation function
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
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.