[R] colClasses

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Jun 21 22:04:25 CEST 2006

"Kerpel, John" <John.Kerpel at infores.com> writes:

> Hi Folks!
> I'm reading in some data from a .csv file that has a date column.
> How do I use colClasses to get read.csv to recognize the date column?
> The documentation on this seems to be nil - 
> And yes, I've read help and R Data Import/Export and can't figure out
> what the colClasses syntax is.

1. It may be easier just to read the date field as factor or character
   and convert using as.Date afterwards. If a format is needed, this
   is actually the only way.

2. colClasses is a character vector, possibly named, so

   cls <- rep(NA,17)
   cls[3] <- "Date"
   read.csv(...., colClasses=cls, ....)


   read.csv(...., colClasses=c(mydate="Date"), ....)

   should work (assuming that the variable in question is called
   "mydate" and is in the 3 of 17 columns). This is documented in

   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907

More information about the R-help mailing list