# [R] Sorting problem

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri Dec 24 19:16:06 CET 2004

```On 24-Dec-04 Gabor Grothendieck wrote:
>> From: Gabor Grothendieck <ggrothendieck at myway.com>
>> > > From: <Ted.Harding at nessie.mcc.ac.uk>
>> > > On 24-Dec-04 Gabor Grothendieck wrote:
>> > > > Actually they are shown in order of the levels of the factor.
>> > > > e.g. to show them in reverse order:
>> > > >
>> > > > attach(InsectSprays)
>> > > > spray<-factor(as.character(spray),level=rev(levels(spray)))
>> > > > boxplot(count ~ spray)
>> > >
>> > > Applying boxplot to data constructed as follows:
>> > >
>> > > X<-list(C=rnorm(10),B=rnorm(20),A=rnorm(30))
>> > > boxplot(X)
>> > >
>> > > gives the plots in the order "C", "B", "A", so here they indeed
>> > > come out in the order of the data.
>> > >
>> > > But suppose I had wanted them in the order "B", "A", "C", say.
>> > > How would Gabor's suggestion (or similar) be applied to this case?
>> >
>> > boxplot(X[c("B","C","A")])
>> >
>>
>> It occurred to me that maybe your question was not how to reorder
>> them if they are in list form but how to convert the list form
>> to the formula form. If that was it then:

Yes, I was interested in how to relate the two forms.

>> Xu <- unlist(X)
>> g <- factor(rep(names(X), sapply(X, length)))
>> # add levels= arg to reorder
>> boxplot(Xu ~ g)

And thanks for this manoeuvre! But ...

> or even shorter:
>
> boxplot(values ~ ind, stack(X))

... Wow!! I always admire neat footwork, but, Gabor, how many
feet have you got? This is like watching the 3-card trick!

Happy Christmas!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861  [NB: New number!]
Date: 24-Dec-04                                       Time: 18:16:05
------------------------------ XFMail ------------------------------

```