[R] Problem working with dates

David Winsemius dwinsemius at comcast.net
Mon Nov 7 15:58:39 CET 2011


On Nov 7, 2011, at 9:39 AM, Paul Miller wrote:

> Hello All,
>
> I've been reading books about R for awhile now and am in the process  
> of replicating the SAS analyses from an old report. I want to be  
> sure that I can do all the things I need to in R before using it in  
> my daily work.
>
> So far, I've managed to read in all my data and have done some data  
> manipulation. I'm having trouble with fixing an error in a date  
> variable though, and was hoping someone could help.
>
> One of the patients in my data has a DOB incorrectly entered as:
>
> '11/23/21931'
>
> Their DOB should be:
>
> '11/23/1931'
>
> How can I correct this problem before calculating age in the code  
> below?
> DOB starts out as a factor in the Demo dataframe but then is  
> converted into a date. So I had thought the ifelse that follows  
> could be used to correct the problem, but this doesn't seem to be  
> the case.
>
> Thanks,
>
> Paul
>

Why not fix the single error first?

Demo[ Demo$Subject==108945, "DateCompleted"] <- '11/23/1931'

Then you can skip the time-consuming ifelse() inside the within() call.

-- 
David

> Demo_Char <- within(Demo, {
> DateCompleted <- as.Date(DateCompleted, format = "%m/%d/%Y")
> DOB <- as.Date(DOB, format = "%m/%d/%Y")
> DOB <- ifelse(Subject==108945, as.Date("1931-11-23"), DOB)
> Age <- as.integer((DateCompleted - DOB) / 365.25)
> })
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list