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)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



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.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

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)