[R] Simple as.Date question dealing with a timezone offset

David Winsemius dwinsemius at comcast.net
Thu Sep 17 20:28:33 CEST 2009


On Sep 17, 2009, at 11:25 AM, esawdust wrote:

>
> I've been trying to understand the as.Date functionality and I have  
> a date
> and time stamp field that looks like this:
>
> "Tue Sep 15 09:22:09 -0600 2009"
>
> and I need to turn it into an R Date object for analysis.
>
> Simple date conversions I have down, no problem:
>
>> adate = c("7/30/1959")
>> as.Date(adate,"%m/%d/%Y")
> [1] "1959-07-30"
>>
>
> But when it comes to the type of date/time string format I have  
> above, I
> can't figure out a format string that will work.
>
> The timezone offset is one part that causes problems.  Building up  
> to a
> working format string for the full time stamp string, I can make it  
> as far
> as:
>
>> adate = c("Tue Sep 15 09:22:09 -0600 2009")
>> as.Date(adate,format="%a %b %d %H:%M:%S")
> [1] "2009-09-15"
>
> (apparently year defaults to current year when it's not specified in  
> the
> format string).  Because the Year comes after the timezone offset, I  
> have to
> deal with the timezone offset in the format string.
>
> But when I get to the timezone offset value I can't use "%z" or "%Z"  
> because
> those are "output only"
>
>> as.Date(adate,format="%a %b %d %H:%M:%S %z %Y")
> [1] NA

You are confusing R Date objects with the the date-time classes. I  
don't think Date classes objects even allow TZ offets.

?DateTimeClasses

 > as.POSIXct(as.Date(adate,"%m/%d/%Y"), origin="1960-01-01", tz="GMT")
[1] "1959-07-29 20:00:00 EDT"'

Notice that my TZ (GMT -4) was used. so it was still the prior day in  
New England.

>
> I'm close, but can't incorporate the timezone offset field in the  
> date/time
> stamp string.
>
> What am I missing?   I suppose one workaround is to split the date/ 
> time
> string into its component parts, reassemble it into a string as.Date  
> can
> deal with, but that seems to defeat one of the purposes of as.Date's  
> format
> capability.
>
> Any advice for how to translate a "Tue Sep 15 09:22:09 -0600 2009"  
> into an R
> Date object?
>
> Landon
>
-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list