[R] unable to subtract dates in R

Rui Barradas ruipbarradas at sapo.pt
Thu Jul 12 01:29:49 CEST 2012


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.
>



More information about the R-help mailing list