Thanks for this feedback. I was able to get that example to work, but I 
have gotten conflicting results with factors converted via POSIXct using 
the format= and tz= options to bring in excel-type dates and times. Here 
is what I have tried:

dt.factor <- factor(c("3/17/2011 14:07","3/20/2011 7:18","3/22/2011 
14:10","3/29/2011 9:07","7/15/2010 7:49"))

>  dt.factor
[1] 3/17/2011 14:07 3/20/2011 7:18  3/22/2011 14:10 3/29/2011 9:07  7/15/2010 
7:49
Levels: 3/17/2011 14:07 3/20/2011 7:18 3/22/2011 14:10 3/29/2011 9:07 7/15/2010 
7:49


>  as.POSIXct(dt.factor,format="%m/%d/%Y %H:%M",tz="CST6CDT")
Error in as.POSIXlt.character(as.character(x)) :
   character string is not in a standard unambiguous format
>  
>  as.POSIXct(as.character(dt.factor),format="%m/%d/%Y %H:%M",tz="CST6CDT")
[1] "2011-03-17 14:07:00 CDT" "2011-03-20 07:18:00 CDT" "2011-03-22 14:10:00 
CDT"
[4] "2011-03-29 09:07:00 CDT" "2010-07-15 07:49:00 CDT"


So, it works only after converting the factor variable to character, but 
it is a simple matter to do that conversion.

Thanks
Steve

On 3/29/2012 3:24 PM, Prof Brian Ripley wrote:
> 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
>>>
>>
>> ______________________________________________
>> [email protected] 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
[email protected] 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.

Reply via email to