[R] Problem working with dates

Paul Miller pjmiller_57 at yahoo.com
Mon Nov 7 15:39:06 CET 2011


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

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)
})



More information about the R-help mailing list