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

Spencer Graves spencer.graves at structuremonitoring.com
Wed Jul 11 23:22:07 CEST 2012

Hi, Prof. Ripley, Bert:

       Thanks for the comments.

       Might there be a function similar to "c" but retains more 
attributes than just names?

       Before I write such, I felt a need to ask if anyone knows where 
it may already have been done -- and if people have suggestions for how 
they think it should function with particular classes of objects?  For 
example, if create an S3 generic "c2" for this, "c2.default" might 
retains all the attributes that make sense from the first argument and 
ignore attributes from later arguments if it makes sense to do so.  This 
would then retain the tzone attribute from the first of multiple POSIXct 


On 7/9/2012 6:24 AM, Prof Brian Ripley wrote:
> 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
>>> ______________________________**________________
>>> 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.

More information about the R-help mailing list