[R] Dates are read as factors!

Marc Schwartz MSchwartz at mn.rr.com
Thu Jul 28 05:25:24 CEST 2005

On Wed, 2005-07-27 at 23:07 -0400, John Sorkin wrote:
> I am using read.csv to read a CSV file (produced by saving an Excel file
> as a CSV file). The columns containing dates are being read as factors.
> Because of this, I can not compute follow-up time, i.e.
> Followup<-postDate-preDate. I would appreciate any suggestion that would
> help me read the dates as dates and thus allow me to calculate follow-up
> time.
> Thanks
> John


Depending upon how may columns you are reading, you could:

1. Use the 'colClasses' argument in read.csv() to specify the data types
for each incoming column, one of which is 'Date'


2. Post process the data after importing by using the as.Date()
function. See ?as.Date for more information and pay particular attention
to the 'format' argument.

My own preference is number 2.

Once the requisite columns are converted to the Date class, you can
engage in date based arithmetic, etc.


Marc Schwartz

More information about the R-help mailing list