The difftime object
does a great job of date and time arithmetic. The problem is that the units it recognizes do not include years. So, in place of randomizing on years, I used weeks, a unit difftime accepts.
# Simulate 10 persons with age evenly distributed 0 to 21 (use weeks)
agewks <- runif(10, 0, (21*52))
# convert to difftime object
agedt <- as.difftime(agewks, units="weeks")
# above could be combined into single step
# agewks <- as.difftime(runif(10, 0, (21*52)), units='weeks')
# subtract from right now to establish a 'birthdate' for our simulated persons
bday <- as.POSIXct(Sys.time() - agedt)
bday
[1] "1997-05-26 13:23:07 EDT" "2003-02-24 13:07:48 EST"
[3] "2006-12-20 12:38:04 EST" "2002-01-02 15:17:14 EST"
[5] "1993-10-07 15:49:19 EDT" "2001-05-04 04:05:29 EDT"
[7] "2003-09-28 09:35:30 EDT" "1996-05-17 20:58:15 EDT"
[9] "2008-08-09 14:17:24 EDT" "2011-05-09 23:26:04 EDT"
# to create a date object use
bday <- as.Date(Sys.time() - agedt)
Thanks to Carl and others for steering me to the above. Alternate and better approaches welcomed.