[R] POSIXlt vs POSIXct

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 29 22:24:58 CEST 2012


On 29/03/2012 21:05, Steven R Corsi wrote:
> That is a good tip. I have tried it and found that it works if I make
> sure it was converted to character first. strptime appears to accept
> factor variables which is the default when reading in the file originally.

I would say that POSIXct is the way to go if you know the timezone of 
the datetimes (it is essential to do the conversion).  But you don't 
always do so, and then POSIXlt can be useful (although assuming UTC can 
also work).

Several times a month we get help requests about times on DST 
transitions which shows that people too often think they know the 
timezone and in fact do not (as the times are invalid or ambiguous in 
the assumed timezone).

as.POSIXct does work with factors ....

z <- "2012-03-29 21:20:05"
 > as.POSIXct(z)
[1] "2012-03-29 21:20:05 BST"
 > as.POSIXct(factor(z))
[1] "2012-03-29 21:20:05 BST"


> Thanks
> Steve
>
> On 3/29/2012 2:31 PM, MacQueen, Don wrote:
>> I also find that POSIXct is generally the most useful, and only use
>> POSIXlt in special cases.
>>
>> But have you considered as.POSIXct() instead of strptime()? It works for
>> me, and I can't remember the last time I had to use strptime() for
>> converting character to date/time. (But I mostly don't work with multiple
>> time zones, except for converting to/from UTC.
>>
>> -Don
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


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