Skip to content

reorder [stats] and reorder.factor [lattice]

4 messages · Warnes, Gregory R, Deepayan Sarkar, Martin Maechler +1 more

#
I also have a reorder.factor defined in the gregmisc package.  It has a
slighly different behavior.
It allows 
	- sorting the factor level names via 'mixedsort', which sorts mixed 
        numeric/character intelligently so that 'Cis 10mg' comes after 
        'Cis 5mg' and before 'Taxol 10mg'
	- reordering by a numeric order 
	- reordering by named factor labels.

Combining it with the version provided by Deepayan we get:

reorder.factor <- function(x,
                           order,
                           X,
                           FUN,
                           sort=mixedsort,
                           make.ordered = is.ordered(x),
                           ... )
  {
    constructor <- if (make.ordered) ordered else factor

    if (!missing(order))
      {
        if (is.numeric(order))
          order = levels(x)[order]
        else
          order = order
      }
    else if (!missing(FUN))
      order = names(sort(tapply(X, x, FUN, ...)))
    else
      order = sort(levels(x))

    constructor( x, levels=order)

  }

Yielding:
+                   100, replace=TRUE ) )
1200 MG  300 MG  600 MG PLACEBO 
     18      24      30      28
300 MG  600 MG 1200 MG PLACEBO 
     24      30      18      28
PLACEBO  300 MG  600 MG 1200 MG 
     28      24      30      18
PLACEBO  300 MG  600 MG 1200 MG 
     28      24      30      18
1200 MG  300 MG PLACEBO  600 MG 
     18      24      28      30
PLACEBO  600 MG 1200 MG    NA's 
     28      30      18      24
-Greg

(the 'mixedsort' function is available in the gregmisc package, or on
request)
LEGAL NOTICE\ Unless expressly stated otherwise, this messag...{{dropped}}
10 days later
#
Before it's too late for R 2.0.0, do we have a final decision yet on 
having a reorder method for "factor" in stats?

Deepayan
On Friday 03 September 2004 11:36, Warnes, Gregory R wrote:
#
DeepS> Before it's too late for R 2.0.0, do we have a final decision yet on 
    DeepS> having a reorder method for "factor" in stats?

Since the thread is quite a bit old, (and I have been in vacation back then),
could you summarize what you think about it?

When skimming through the thread I got the impression that, yes,
it was worth to "centralize" such a method in 'stats' rather
than have different slightly incompatible versions in different
other packages.
This is of tangential interest to me as I have been slightly
involved with reorder.dendrogram()

Regards,
Martin
#
Summary: I have just put a version in stats as supplied by Deepayan.  It
is more or less the same as that in Trellis (there never was one in
lattice AFAIK), except that the result is by default only ordered if the
input was, and the `scores' used to do the ordering are returned. (Both 
points I raised in earlier discussions.)

Package gregmisc has a different function of that name which appears to be
more related to relevel and levels<-.
On Tue, 14 Sep 2004, Martin Maechler wrote: