patrick.guevel at uk.bnpparibas.com
2006-Jun-06 17:10 UTC
[R] Suspicious behaviour of sort on POSIXct vectors in R-2.3.0 and R-2.3.1
Hi , When I sort a vector of POSIXct values in R-2.3.0 and R-2.3.1, I get a vector of numeric values and this gets some of my code to crash (class object creation). Is that a R bug? In the mean time. I'll try to override the sort function for the POSIXct objects. Thanks, Patrick Gu?vel Head of quantitative research for electronic trading This message and any attachments (the "message") is\ intende...{{dropped}}
Gabor Grothendieck
2006-Jun-06 18:20 UTC
[R] Suspicious behaviour of sort on POSIXct vectors in R-2.3.0 and R-2.3.1
Use order instead: x <- as.POSIXct(Sys.Date() + 10:1) x[order(x)] On 6/6/06, patrick.guevel at uk.bnpparibas.com <patrick.guevel at uk.bnpparibas.com> wrote:> Hi , > > When I sort a vector of POSIXct values in R-2.3.0 and R-2.3.1, I get a > vector of numeric values and this gets some of my code to crash (class > object creation). Is that a R bug? > > In the mean time. I'll try to override the sort function for the POSIXct > objects. > > Thanks, > > Patrick Gu?vel > Head of quantitative research for electronic trading > > > This message and any attachments (the "message") is\ intende...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Prof Brian Ripley
2006-Jun-06 20:14 UTC
[R] Suspicious behaviour of sort on POSIXct vectors in R-2.3.0 and R-2.3.1
On Tue, 6 Jun 2006, patrick.guevel at uk.bnpparibas.com wrote:> Hi , > > When I sort a vector of POSIXct values in R-2.3.0 and R-2.3.1, I get a > vector of numeric values and this gets some of my code to crash (class > object creation). Is that a R bug?No, it is as documented: see ?sort As from R 2.3.0, all attributes are removed from the return value except names, which are sorted. (If 'partial' is specified even the names are removed.) Note, the class is an attribute. For many classes sorting destroys the appropriateness of the class.> In the mean time. I'll try to override the sort function for the POSIXct > objects.You can restore the class if appropriate. -- 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
Martin Maechler
2006-Jun-07 15:30 UTC
[Rd] sort() generic? [Re: Suspicious behaviour of sort on POSIXct ..]
{Diverted to R-devel}>>>>> "BDR" == Prof Brian Ripley <ripley at stats.ox.ac.uk> >>>>> on Tue, 6 Jun 2006 21:14:01 +0100 (BST) writes:BDR> On Tue, 6 Jun 2006, patrick.guevel at uk.bnpparibas.com BDR> wrote: >> Hi , >> >> When I sort a vector of POSIXct values in R-2.3.0 and >> R-2.3.1, I get a vector of numeric values and this gets >> some of my code to crash (class object creation). Is that >> a R bug? BDR> No, it is as documented: see ?sort BDR> As from R 2.3.0, all attributes are removed from BDR> the return value except names, which are sorted. (If BDR> 'partial' is specified even the names are removed.) BDR> Note, the class is an attribute. For many classes BDR> sorting destroys the appropriateness of the class. Indeed, and I agree this a good change. However, the above also suggests that ideally, sort() would be a generic function. One good reason for sort() not being generic now is the fact that method dispatch costs a bit, *and* that we like sort() to be really fast. One way to achieve a generic sort() and keep the possibility of of very fast sort() --- similarly to rep() and rep.int() would be to rename the current sort into something like sortNum() {"Num" for numeric}, make sort() into a generic, and replace sort() by sortNum() in those code parts which need to remain optimally fast. >> In the mean time. I'll try to override the sort function >> for the POSIXct objects. BDR> You can restore the class if appropriate.