[R] converting DOB format to age

arun smartpink111 at yahoo.com
Tue Oct 9 03:54:14 CEST 2012


Hi,
Try either of these:
DOB1<-"08/22/1976"
 as.numeric(format(as.Date(Sys.time()),format="%Y"))-as.numeric(format(as.Date(DOB1,format="%m/%d/%Y"),format="%Y"))
#[1] 36

library(zoo)
as.numeric(gsub(".*\\s","",as.yearmon(Sys.time())))-as.numeric(gsub(".*\\s","",as.yearmon(DOB1,format="%m/%d/%Y")))
#[1] 36

library(lubridate)
as.period(Sys.time()-mdy(DOB1),units="year")
# 1 parsed with %m/%d/%Y
#estimate only: convert difftimes to intervals for accuracy
#[1] "36y 0m 48d 1H 49M 26.6576771736145S"
A.K.




----- Original Message -----
From: farnoosh sheikhi <farnoosh_81 at yahoo.com>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc: 
Sent: Monday, October 8, 2012 2:26 PM
Subject: [R] converting DOB format to age

Hi,

I have a column of DOB in mm/dd/yyyy. I want to convert this format to age.
Thanks a lot.
 
Best,Farnoosh Sheikhi
    [[alternative HTML version deleted]]


______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list