[R] sorting question

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jul 1 19:27:06 CEST 2009


This maps each string to one of the form yearQqtr at which point
you can sort them.   Modify the mapping as necessary.

> library(gsubfn)
> dd <- c("2002", "2003H1", "2003H2", "2004", "2005Q1", "2005Q2")

> gsubfn("H.|Q.|$", list(H1 = "Q1", H2 = "Q2", Q2 = "Q2", Q3 = "Q3", Q4 = "Q4", "Q1"), dd)
[1] "2002Q1" "2003Q1" "2003Q2" "2004Q1" "2005Q1" "2005Q2"

See http://gsubfn.googlecode.com for more.

On Wed, Jul 1, 2009 at 12:58 PM, Steve Jaffe<sjaffe at riskspan.com> wrote:
>
> I've asked about custom sorting before and it appears that -- in terms of a
> user-defined order -- it can only be done either by defining a custom class
> or using various tricks with "order"
>
> Just wondering if anyone has a clever way to order "vintages" of the form
> 2002, 2003H1, 2003H2, 2004,  2005Q1, 2005Q2, etc
> some have H1 or H2, some have Q1,Q2,Q3,Q4, some are just plain years. They
> should be sorted in the obvious order. I can think of doing something with
> s'trsplit' and 'order' but anyone have anything better?
>
> (I still wonder why sorting with a user-defined function isn't supported.  I
> guess I should follow the open source philosophy and contribute my own, but
> it seems that would involve implementing an explicit, iterative sort
> algorithm, whereas it would make more sense for it to be  integrated with
> the internal sort function, if that were possible)
>
> Thanks
> --
> View this message in context: http://www.nabble.com/sorting-question-tp24293430p24293430.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list