[R] strptime and non ISO date format

Dirk Eddelbuettel edd at debian.org
Fri May 16 02:24:54 CEST 2003


On Fri, May 16, 2003 at 01:36:04AM +0200, Petr Pikal wrote:
> I have a character vector of dates something like:
> 
> timevec<-c("15.5.2003 00:00", "15.5.2003 00:01", "15.5.2003 00:02", 
> "15.5.2003 00:03","15.5.2003 00:04")
> 
> and I would like to transform it to some more convenient date class. Is there a 
> way how to do it directly without previous reformating to ISO like structure and 
> adding a seconds to it.
> 
> I tried direct transformation but it does not work as I expected (or as I 
> understood from help pages :-).
> 
> > strptime(timevec,"%m.%d.%y %H:%M")
> [1] NA NA NA NA NA

You have the month and day mixed up, and also asked for two-digit year when
you really have four digits. Correcting both of these does the trick:

edd at chibud:~> R --silent
> timevec<-c("15.5.2003 00:00", "15.5.2003 00:01", "15.5.2003 00:02",
+ "15.5.2003 00:03","15.5.2003 00:04")
> strptime(timevec,"%d.%m.%Y %H:%M")
[1] "2003-05-15 00:00:00" "2003-05-15 00:01:00" "2003-05-15 00:02:00"
[4] "2003-05-15 00:03:00" "2003-05-15 00:04:00"

Hth, Dirk

-- 
Don't drink and derive. Alcohol and algebra don't mix.




More information about the R-help mailing list