[R] two different date formats in the same variable

Farrel Buchinsky fjbuch at gmail.com
Tue Mar 24 01:06:58 CET 2009


How does one convert to a date format when survey respondents have
used two different date formats whilst entering their data. There were
clearly told to use mm/dd/yyyy but humans being humans some entered
mm/dd/yy. There was even validity checks on the forms but I allowed
them to be overridden since the data is more holy than the format.

The data was downloaded as a csv and read.csv was used to read in.
There are several date variables (for example date of birth, date of
diagnosis). Some became character vectors and others become factor
vectors. Nevertheless I have accomplished most of what I want using
lines such as

strptime(init.consent$consent.rec,"%m/%d/%Y")
strptime(x,"%m/%d/%Y")
 as.Date(x, "%m/%d/%Y")

But what happens when a few of the entries get messed up because they
are actually formatted %m/%d/%y.

Is there a robust date formatter? Alternatively how would one code
(presumably using regular expressions) a transforamtion or
substitution only on the errant entries and thereby turn 06/25/04 into
06/25/2004 and 03/03/59 into 03/03/1959?

sessionInfo()
R version 2.8.1 (2008-12-22)
i386-pc-mingw32


Farrel Buchinsky




More information about the R-help mailing list