When reading a file, the read.table
function uses type.convert
to distinguish between logical, integer, numeric, complex, or factor columns and store them accordingly.
I'd like to add dates to the mix, so that columns containing dates can automatically be recognized and parsed into Date
objects. Only a few date formats should be recognized, e.g.
date.formats <- c("%m/%d/%Y", "%Y/%m/%d")
Here is an example:
fh <- textConnection(
"num char date-format1 date-format2 not-all-dates not-same-formats
10 a 1/1/2013 2013/01/01 2013/01/01 1/1/2013
20 b 2/1/2013 2013/02/01 a 2013/02/01
30 c 3/1/2013 NA b 3/1/2013"
)
And the output of
dat <- my.read.table(fh, header = TRUE, stringsAsFactors = FALSE,
date.formats = date.formats)
sapply(dat, class)
would give:
num => numeric
char => character
date-format1 => Date
date-format2 => Date
not-all-dates => character
not-same-formats => character # not a typo: date format must be consistent
Before I go and implement it from scratch, is something like this already available in a package? Or maybe someone already gave it a crack (or will) and is willing to share his code here? Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…