Saturday 24 August 2013

Determine birthdate from age in r

Determine birthdate from age in r

Assigning a birth date to a simulated sample; the following works, but
ignores leap years.
Wondered if there is a more precise (and elegant) r approach?
# Simulate 10 persons with age evenly distributed 0 to 21
age <- runif(10, 0, 21)
# calc age in seconds
agesecs <- age*365*24*60*60
# subtract from right now to establish 'birthdate'
bday <- as.Date(Sys.time() - agesecs)
bday
[1] "2008-03-28" "1998-06-12" "2010-05-02" "2007-01-11" "2007-06-07"
[6] "1999-05-22" "2004-01-29" "2013-03-29" "1998-06-01" "2006-10-14"

No comments:

Post a Comment