[Rd] sort changes datatype of operand

Henrik Bengtsson hb at stat.berkeley.edu
Fri Aug 18 09:36:57 CEST 2006


On 8/18/06, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> Please do note that this too is a reading error. The documentation for
> rowsum() says
>
>    group: a vector giving the grouping, with one element per row of
>           'x'.
>
> R has used 'group' as if it were as.vector(group), as it was entitled to
> do.  If you do not want that, give a _vector_ with the property you do
> want, e.g. as.character(group).
>
> You could also call rowsum() with reorder=FALSE and reorder later to your
> taste.
>
> On Thu, 17 Aug 2006, Brahm, David wrote:
>
> > On 8/3/2006 10:34 AM, <stephen.ponzio at citigroup.com> noted that,
> > starting with R-2.3.0, sort() fails to preserve date classes:
> >
> > > dates <- seq(Sys.Date(), len=5, by="1 day")
> > > dates[order(dates)]
> > > [1] "2006-08-03" "2006-08-04" "2006-08-05" "2006-08-06" "2006-08-07"
> > > sort(dates)
> > > [1] 13363 13364 13365 13366 13367
> >
> > and Duncan Murdoch <murdoch at stats.uwo.ca> replied:
> >
> > > The problem is that some classes assume a particular ordering for
> > > values; sort can mess them up.  If you know that's not the case, you
> > > can protect the class yourself:
> > >   cl <- class(dates)
> > >   sorteddates <- sort(dates)
> > >   class(sorteddates) <- cl

Just curious, what is it about the Date class that make it assume a
"particular ordering" that sort() can mess up?  Why is it wrong to
have the following?

sort.default <- base::sort
sort <- function(...) UseMethod("sort")
sort.Date <- function(x, ...) { y <- NextMethod("sort", x, ...);
class(y) <- class(x); y }

/Henrik

> >
> > I have to agree with Stephen (and Alex Dannenberg in another post)
> > that this change is unfortunate for date classes.  How do you
> > reproduce this old behavior (without assuming alphanumeric ordering):
> >
> > > x <- matrix(1:12, 4,3)
> > > group <- Sys.Date() + c(0,1,0,1)
> > > rowsum(x, group)
> >            [,1] [,2] [,3]
> > 2006-08-17    4   12   20
> > 2006-08-18    6   14   22
> >
> > Under R-2.3.1, the result is now:
> >       [,1] [,2] [,3]
> > 13377    4   12   20
> > 13378    6   14   22
> >
> > Blech!
> >
> > -- David Brahm (brahm at alum.mit.edu)
>
> --
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



More information about the R-devel mailing list