s-news
[Top] [All Lists]

Re: How to calculate someone's age

To: s-news@wubios.wustl.edu
Subject: Re: How to calculate someone's age
From: Wim Kimmerer <kimmerer@sfsu.edu>
Date: Wed, 15 Dec 2004 21:26:17 -0800
If x is the data frame given by Tony Plate, the following gives the same result the way your cake decorator (or 6-year-old) would do it: subtract the years except for the last one if today's date is earlier than the birth date.  Without those unsightly loops:

dy <- as.numeric(as.character(years(x$Event))) - as.numeric(as.character(years(x$DOB)))
vm <- as.numeric(months(x$Event)) - as.numeric(months(x$DOB))
vd <- as.numeric(days(x$Event)) - as.numeric(days(x$DOB))
v <- ifelse(vm>0,0,ifelse(vm==0 & vd >=0,0,1))

x$dy <- dy - v
all(x$dy == x$ans)



x
        Event        DOB ans dy
 1 07/11/2004 01/22/1965  39 39
 2 06/12/2004 02/12/1974  30 30
 3 08/11/2004 08/01/1959  45 45
 4 02/28/2005 02/28/2004   1  1
 5 02/28/2004 02/28/2003   1  1
 6 02/27/2005 02/28/2004   0  0
 7 02/27/2004 02/28/2003   0  0
 8 03/01/2005 02/28/2004   1  1
 9 02/29/2004 02/28/2003   1  1
10 03/01/2005 03/01/2004   1  1
11 03/01/2004 03/01/2003   1  1
12 02/29/2004 03/01/2003   0  0
13 03/01/2005 03/01/2004   1  1
14 02/28/2004 03/01/2003   0  0



>NTeuscher@npsp.com wrote:
>
>>I'm an SPlus newbie, so I apologize for such a simple question, but I
>>can't seem to find the answer in a manual or on-line.
>>I have two columns of dates (MM/DD/YYYY) as follows:
>>Event                     DOB
>>07/11/2004          01/22/1965
>>06/12/2004          02/12/1974
>>08/11/2004          08/01/1959
>>I need to calculate the age for each row and have the output in years.
<Prev in Thread] Current Thread [Next in Thread>