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

esawdust landon at 360vl.com
Thu Sep 17 17:25:38 CEST 2009


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

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

-- 
View this message in context: http://www.nabble.com/Simple-as.Date-question-dealing-with-a-timezone-offset-tp25491955p25491955.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list