Hi, It is a bit difficult to give suggestions when there are no example datasets. Have you checked whether both belong to "Date" class?
You could use, difftime(intakeDS$DOB, intakeDS$IntakeDate_d,units="days") to get the difference between dates A.K. ----- Original Message ----- From: William Mabe <billm...@gmail.com> To: r-help@r-project.org Cc: Sent: Wednesday, July 11, 2012 6:20 PM Subject: [R] unable to subtract dates in R Hi, I wanted to calculate the age of people in my dataset by subtracting the individual's date of birth from their intake into a program. After several hours, searches of help archives, and the downloading of lubiridate, I have had no luck with this. Below is the code I used. > intakeDS$DOB <- as.character(intakeDS$DOB) > intakeDS$DOB <- as.Date(intakeDS$DOB, "%Y%m%d") > > # Make IntakeDate a date variable, by first converting it from numeric to character then from character to date > #intakeDS$IntakeDate <- as.character(intakeDS$IntakeDate) > intakeDS$IntakeDate_d <- as.Date(intakeDS$IntakeDate_c, "%Y%m%d") > > intakeDS$intake_age <- intakeDS$DOB - intakeDS$IntakeDate_d > intakeDS$intake_age Time differences in days [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [61] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [121] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [181] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA [241] NA NA NA NA NA NA NA I also downloaded the lubridate package and used the new_interval command and that created a variable that showed the start date and the end date. However, I was not able to subtract one date from the other to calculate an age variable. Any help would be greatly appreciated as this has been a pretty miserable afternoon hacking away at this. Thanks, Bill [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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.