[R] strptime format argument

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 29 00:08:27 CEST 2008


On Mon, 28 Apr 2008, Hutchinson,David [PYR] wrote:

> I am getting an unanticipated result attempting to format a datetime
> string
>
> date1 <- strptime('2005 297 1030 35', '%Y %j %H%M %S')     # returns
> "2005-10-24 10:12:35"
> date2 <- strptime('2005 297 912 35', '%Y %j %H%M %S')      # returns NA
>
> Are there any ways to adjust the format argument (e.g., '%H') to accept
> one or two digit hours?

That's not the issue:

> strptime('2005 297 9 12 35', '%Y %j %H %M %S')
[1] "2005-10-24 09:12:35"

The issue is that 91 is an invalid value for %H -- it reads from left to 
right

> strptime('2005 297 112 35', '%Y %j %H%M %S')
[1] "2005-10-24 11:02:35"

You will need to preprocess the string if you want another interpretation.

-- 
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