Here I threw one together quickly. It is not handling the last column properly because the as.Date
function is not strict enough (see that as.Date("1/1/2013", "%Y/%m/%d")
parses ok for example...)
my.read.table <- function(..., date.formats = c("%m/%d/%Y", "%Y/%m/%d")) {
dat <- read.table(...)
for (col.idx in seq_len(ncol(dat))) {
x <- dat[, col.idx]
if(!is.character(x) | is.factor(x)) next
if (all(is.na(x))) next
for (f in date.formats) {
d <- as.Date(as.character(x), f)
if (any(is.na(d[!is.na(x)]))) next
dat[, col.idx] <- d
}
}
dat
}
dat <- my.read.table(fh, header = TRUE, stringsAsFactors = FALSE)
as.data.frame(sapply(dat, class))
# sapply(dat, class)
# num integer
# char character
# date.format1 Date
# date.format2 Date
# not.all.dates character
# not.same.formats Date
If you know a way to parse dates that is more strict around formats than as.Date
(see the example above), please let me know.
Edit: To make the date parsing super strict, I can add
if (!identical(x, format(d, f))) next
For it to work, I will need all my input dates to have leading zeroes where needed, i.e. 01/01/2013
and not 1/1/2013
. I can live with that if that's the standard way.