RE: [Rd] proposed change to [.POSIXct

From: Whit Armstrong <Whit.Armstrong_at_tudor.com>
Date: Fri 20 Aug 2004 - 02:53:49 EST

 

> Sorry, but you really do not want that the change your patch
> makes. Copying across the attributes will for example copy
> the original names, any dim's etc. There is a very good
> reason why [ does not do that itself.

Copying all attributes may very well be troublesome. Perhaps the approach taken in [.difftime or in [.times would be more appropriate:

> get("[.difftime")

function (x, ..., drop = TRUE)
{

    cl <- oldClass(x)
    class(x) <- NULL
    val <- NextMethod("[")
    class(val) <- cl
    attr(val, "units") <- attr(x, "units")     val
}
<environment: namespace:base>
> get("[.times")

function (x, ..., drop = TRUE)
{

    cl <- class(x)
    class(x) <- NULL
    val <- NextMethod("[")
    attr(val, "format") <- attr(x, "format")     attr(val, "origin") <- attr(x, "origin")     class(val) <- cl
    val
}

Likewise, the new [.POSIXct would be:

"[.POSIXct" <-
function (x, ..., drop = TRUE)
{

    old.tzone <- attr(x,"tzone")
    old.class <- class(x)
    class(x) <- NULL
    val <- NextMethod("[")
    class(val) <- old.class
    attr(val,"tzone") <- old.tzone
    val
}

The functions [.difftime and [.times preserve attributes when it's sensible to do so. I think POSIXct objects should follow the same rule.  

> I am not even sure you want to preserve the "tzone"
> attribute. A POSIXct time is an absolute time, and that
> attribute is just a reminder of where it came from. Since
> you have operated on it, the history is broken.

I think it should be preserved because in doing a subset operation does not change the elements of an object, it just selects certain elements. I'm not sure what you mean about the history being broken.  

> As timezones are always used when printing, I don't see
> anything problematic about your example. Do you have a more
> convincing example from `which has cause several problems for
> me in the past'?

My problems arose from trying to use the seq function with POSIXct objects.

Here is a toy example:

start <- ISOdate(1952,03,01,hour=0)
end <- ISOdate(2004,03,01,hour=0)

xx <- seq(start,end,by="months")
yy <- seq(start[1],end[1],by="months")

all(as.numeric(xx)==as.numeric(yy))
as.numeric(xx)==as.numeric(yy)

In my opinion, these two sequences should be the same. Since subsetting the vectors causes their printed values to be different, the whole sequence is thrown off even though the underlying values for start and end are the same:

> unclass(start)

[1] -562896000
attr(,"tzone")
[1] "GMT"
> unclass(start[1])

[1] -562896000

> unclass(end)

[1] 1078099200
attr(,"tzone")
[1] "GMT"
> unclass(end[1])

[1] 1078099200
>

I hope this case is sufficient for you to consider this proposed change. The r-list contains frequent posts by people who don't understand the underlying implementation of dates in R. I think this change will reduce the cases in which unsuspecting users have their tzone attribute stripped because of a subset operation.

Thanks again for your time in addressing this issue Professor Ripley.

Cheers,
Whit

>
> On Thu, 19 Aug 2004, Whit Armstrong wrote:
>
> > R developers,
> >
> > The "tzone" attribute is stripped from a POSIXct object when the
> > subscript command is called ("[.POISXct"). This results in dates
> > being printed in the locale specific format after a subscript
> > operation is applied to a POSIXct object which has cause several
> > problems for me in the past.
> >
> > Here is an example of this problem under R 1.9.1:
> >
> > > x <- ISOdate(rep(2000,2),rep(3,2),rep(26,2),hour=0)
> > > x
> > [1] "2000-03-26 GMT" "2000-03-26 GMT"
> > > x[1]
> > [1] "2000-03-25 19:00:00 Eastern Standard Time"
> > >
> > > unclass(x)
> > [1] 954028800 954028800
> > attr(,"tzone")
> > [1] "GMT"
> > > unclass(x[1])
> > [1] 954028800
> > >
> >
> > Here is the current code for "[.POSIXct"
> >
> > > get("[.POSIXct")
> > function (x, ..., drop = TRUE)
> > {
> > cl <- oldClass(x)
> > class(x) <- NULL
> > val <- NextMethod("[")
> > class(val) <- cl
> > val
> > }
> > <environment: namespace:base>
> > >
> >
> > I believe a sensible change is the following:
> >
> > "[.POSIXct" <-
> > function (x, ..., drop = TRUE)
> > {
> > at <- attributes(x)
> > class(x) <- NULL
> > val <- NextMethod("[")
> > attributes(val) <- at
> > val
> > }
> >
> > Using this code, the "tzone" attribute is preserved
> allowing dates to
> > continue to be printed in their intended timezone.
> >
> > Thank you for considering this issue.
> >
> > Regards,
> > Whit Armstrong
> >
> > Here are the details of my system:
> >
> > > R.Version()
> > $platform
> > [1] "i386-pc-mingw32"
> >
> > $arch
> > [1] "i386"
> >
> > $os
> > [1] "mingw32"
> >
> > $system
> > [1] "i386, mingw32"
> >
> > $status
> > [1] ""
> >
> > $major
> > [1] "1"
> >
> > $minor
> > [1] "9.1"
> >
> > $year
> > [1] "2004"
> >
> > $month
> > [1] "06"
> >
> > $day
> > [1] "21"
> >
> > $language
> > [1] "R"
> >
> > >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-devel@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> >
>
> --
> Brian D. Ripley, ripley@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
>



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri Aug 20 03:12:20 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:45:07 EST