[R] c(a, b) for POSIXct objects with tzone attributes?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 9 15:24:06 CEST 2012


On 09/07/2012 07:12, Bert Gunter wrote:
> Spencer:
>
> Just for fun, may I hazard a guess: It would be messy to retain time zones
> if your were concatenating objects with more than one time zone among them.

Actually, impossible as the design allows for only one timezone for each 
object.

> "Normalizing"  everything to a single zone probably also makes subsequent
> operations on the results much easier.

We considered having c() retain the timezone if it was common to all the 
objects, but the main issue was that c() was documented to remove 
attributes:

      ‘c’ is sometimes used for its side effect of removing attributes
      except names, for example to turn an array into a vector.
      ‘as.vector’ is a more intuitive way to do this, but also drops
      names.  Note too that methods other than the default are not
      required to do this (and they will almost certainly preserve a
      class attribute).

So, sometimes removing and sometimes retaining attributes was going to 
be confusing.

But in any case, the documentation (?c.POSIXct) is clear:

      Using ‘c’ on ‘"POSIXlt"’ objects converts them to the current time
      zone, and on ‘"POSIXct"’ objects drops any ‘"tzone"’ attributes
      (even if they are all marked with the same time zone).

So the recommended way is to add a "tzone" attribute if you know what 
you want it to be.  POSIXct objects are absolute times: the timezone 
merely affects how they are converted (including to character for printing).

>
> Not that it couldn't be done -- and maybe already has been in some package.
> If so, I would bet it's done via S4 classes, which would seem to be a
> natural way to go.
>
> Hopefully, you'll get a more informed and authoritative explanation,
> though. ... I'm just speculating and and may be all wet.
>
> Cheers,
> Bert
>
> On Sun, Jul 8, 2012 at 9:46 PM, Spencer Graves <
> spencer.graves at structuremonitoring.com> wrote:
>
>> Hello:
>>
>>
>>        What is the recommended method for retaining the tzone attributes
>> when concatonating POSIXct objects?
>>
>>
>>   (d1 <- ISOdate(1970,1,1)) # Sets the tzone attribute = GMT
>>>
>> [1] "1970-01-01 12:00:00 GMT"
>>
>>> (d1.2 <- c(d1, d1)) # c(..) strips the tzone attribute, displays in
>>>
>> the time zone of the operating system
>> [1] "1970-01-01 04:00:00 PST" "1970-01-01 04:00:00 PST"
>>
>>> attr(d1.2, 'tzone') <- 'GMT'
>>> d1.2 # tzone attribute manually restored
>>>
>> [1] "1970-01-01 12:00:00 GMT" "1970-01-01 12:00:00 GMT"
>>
>>
>>        "c" is a generic function with a method defined for objects of class
>> POSIXct, so the results here were what is returned by c.POSIXct [as
>> discussed with help('c.POSIXct')].
>>
>>
>>        Is there some other function like "c2", say, that tries to retain
>> attributes where "c" strips all but names?
>>
>>
>>        Thanks,
>>        Spencer
>>
>> --
>> Spencer Graves, PE, PhD
>> President and Chief Technology Officer
>> Structure Inspection and Monitoring, Inc.
>> 751 Emerson Ct.
>> San José, CA 95126
>> ph:  408-655-4567
>> web:  www.structuremonitoring.com
>>
>> ______________________________**________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
>
>
> ______________________________________________
> 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