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 tocharacter 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_ageTime 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]]
Hello, 1. Post a data example, dput(head(intakeDS, 20)) # post the output of this 2. You are subtracting IntakeDate from DOB, when it should be the other way around. Like this you get negative dates and those are illegal. If this point 2 doesn't solve it, follow step 1. Hope this helps, Rui Barradas Em 11-07-2012 23:20, William Mabe escreveu:> 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 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. >
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 <billmabe at gmail.com> To: r-help at 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 tocharacter 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_ageTime 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 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.
Hi William, Glad to know that. So, I guess "difftime()" did the trick if both befong to the class "Date". A.K. ________________________________ From: William Mabe <billmabe at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Thursday, July 12, 2012 1:12 PM Subject: Re: [R] unable to subtract dates in R Thanks Arun! That worked. On Wed, Jul 11, 2012 at 8:17 PM, arun <smartpink111 at yahoo.com> wrote: 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 <billmabe at gmail.com> >To: r-help at 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 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. > >