3

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"
  • 2
    It would be more accurate if you used 365.25 – IRTFM Aug 25 '13 at 01:13
  • See if these links help, you might have to find a way to 'reverse-engineer' what they do. [link1](http://stackoverflow.com/questions/1572110/how-to-calculate-age-in-years-based-on-date-of-birth-and-getdate) [link2](http://stackoverflow.com/questions/3611314/calculating-ages-in-r) – phg Aug 25 '13 at 09:37
  • 2
    The web is littered with horror stories of people who don't properly calculate dates (not to mention a certain thing called "Y2K"). For your sample case, just converting `Sys.time` to year-month-day-hour-min-sec format and then converting your random "years" to the same format will let you use built-in time functions to get the birthdate. Programmer's Rule Number [something]: never reinvent a function that was certainly done before by someone else. – Carl Witthoft Aug 25 '13 at 16:19
  • DWin's solution is certainly quick and 'good enough' for my purpose. That purpose, to answer Fernando's question, is to create a simulated set of records which are similar to an actual set, but in no way derived directly or the result of a mask of actual records. It was Carl's advice which sent me in the direction of a solution. – Anthony Simon Mielniczuk Aug 29 '13 at 00:41
  • Does it have to be base R? A solution can be made with the lubridate package – Victor HDC Sep 06 '13 at 15:13

1 Answers1

0

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.