Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derive Age where DOB = Feb 29th

Tags:

r

I'm looking for a method to derive age that will accurately handle the date of birth = Feb 29th. I've tried using the new_interval function on the lubridatepackage, however for a child born on 29-Feb-2004, this returns an age of 2 years old on 28-Feb-2006. The child would be 2 years old on 01-Mar-2006.
I've also tried using the decimal_date function, but that gives me an error when date = 01-Jan-2006.

I'm using R version 2.15.3 (2013-03-01) -- "Security Blanket"
Here is my code:

library (lubridate)

MyTable <- data.frame(Eval.Date = c(as.Date("2006-01-01"), 
                                    as.Date(0:5, origin = "2006-02-26")),
                      Birth.Date = as.Date("2004-02-29"))

MyTable$Age <- floor(new_interval(MyTable$Birth.Date, MyTable$Eval.Date) / 
                     duration(num = 1, units = "years"))

MyTable$DecDate <- decimal_date(MyTable$Eval.Date)

MyTable[,c("Birth.Date","Eval.Date","Age","DecDate")]

Output:

  Birth.Date  Eval.Date Age  DecDate
1 2004-02-29 2006-01-01   1      NaN
2 2004-02-29 2006-02-26   1 2006.153
3 2004-02-29 2006-02-27   1 2006.156
4 2004-02-29 2006-02-28   2 2006.159
5 2004-02-29 2006-03-01   2 2006.162
6 2004-02-29 2006-03-02   2 2006.164
7 2004-02-29 2006-03-03   2 2006.167

Help either with decimal_date or the age calculation will be appreciated.
Thanks!

like image 918
Freda K Avatar asked Mar 18 '13 21:03

Freda K


People also ask

How is the age calculated for people born on 29th Feb?

His legal thinking is that February 29 is the day after February 28, so a person born on February 29 is legally considered to have aged one year on the day after February 28. In non-leap years, that day is March 1.

When your birthday is February 29th?

Leapling BirthdaysLeap year day on February 29 occurs nearly every four years. However, leap day babies, (leaplings, leapers, or leapsters) still get to celebrate their birthday in common years. Some celebrate on February 28, some prefer March 1.

Do you age if you were born on a leap year?

Even if your birthday is 2/29 you would still be whatever age you were. You would simply observe your birthday on 2/28 or 3/1 in non-leap years.

How old would you be if you were born February 29 2000?

1996: You'd be 24 years old or 6. 2000: You'd be 20 years old or 5. 2004: You'd be 16 years old (the legal age to drive in the U.S.) or 4. 2008: You'd be 12 years old or 3.


1 Answers

Using brute force:

library(lubridate)

age <- function(ED, BD) {
    year(ED) - year(BD) - 1 +
    (month(ED) > month(BD) | 
       (month(ED) == month(BD) & day(ED) >= day(BD)))
}

transform(MyTable, age = age(Eval.Date, Birth.Date))
#    Eval.Date Birth.Date age
# 1 2006-01-01 2004-02-29   1
# 2 2006-02-26 2004-02-29   1
# 3 2006-02-27 2004-02-29   1
# 4 2006-02-28 2004-02-29   1
# 5 2006-03-01 2004-02-29   2
# 6 2006-03-02 2004-02-29   2
# 7 2006-03-03 2004-02-29   2
like image 142
Josh O'Brien Avatar answered Sep 19 '22 10:09

Josh O'Brien