[R] parsing dates in input file

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Dec 13 18:28:39 CET 2007

On Thu, 13 Dec 2007, Tim Coote wrote:

> I've hunted around to try to work this out and cannot find anything
> aposite, although there are exhortations to read News files and the fine
> manual in response to similar queries.  Hope I haven't missed anything
> obvious.
> I want to read in csv files that contain dates, or date times. If I read
> them in directly:
> x <- read.csv ("file")
> the relevant dataframe column is of type string. It would be nice to

type 'character', no doubt.

> read in in one go, but not necessary. If I parse the data and then try
> to overwrite the original column of the data frame:
> y <- strptime (x$datefield, "%d/%m/%Y")
> x$datefield <- y
> I get an error:
> Error in `$<-.data.frame`(`*tmp*`, "datefield", value = list(sec = c(0,
> :
>  replacement has 9 rows, data has 16
> y has 16 values, as does x$datefield.
> I'm sure that this is a common problem.  Has anyone got any pointers?

See  ?as.Date : strptime() does not produce Dates.

Also, ?read.csv says there is an argument

colClasses: character.  A vector of classes to be assumed for the
           columns.  Recycled as necessary, or if the character vector
           is named, unspecified values are taken to be 'NA'.

           Possible values are 'NA' (when 'type.convert' is used),
           '"NULL"' (when the column is skipped), one of the atomic
           vector classes (logical, integer, numeric, complex,
           character, raw), or '"factor"', '"Date"' or '"POSIXct"'.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list