[Rd] reorder [stats] and reorder.factor [lattice]

Deepayan Sarkar deepayan at stat.wisc.edu
Fri Aug 27 21:31:43 CEST 2004


On Friday 27 August 2004 11:17, Prof Brian Ripley wrote:
> On Fri, 27 Aug 2004, Deepayan Sarkar wrote:
> > It was recently pointed out on the lists that the S-PLUS Trellis suite
> > has a function called reorder.factor that's useful in getting useful
> > ordering of factors for graphs. I happily went ahead and implemented it,
> > but it turns out that R (not S-PLUS) has a generic called reorder (with a
> > method for "dendrogram"). Naturally, this causes R to think I'm defining
> > a method for "factor", and gives a warning during check because of
> > mismatching argument names.
> >
> > Any suggestions as to what I should do? Retaining S compatibility doesn't
> > seem to be an option. I could make a reorder method for "factor" (which
> > sounds like a good option to me), or rename it to something like
> > reorderFactor.
>
> I am pretty sure you don't want to copy the Trellis call, which is
>
> function(Factor, X, Function = mean, ...)
>
> and suggests it dates from the days when S3 lookup could not distingush
> functions from other objects by context, hence the capitalization.  Even
> then, it is inconsistent with tapply etc which use FUN.
>
> reorder.factor <- function(x, X, FUN=mean)
>
> looks about right.  Another problem though: in Trellis reorder.factor
> doesn't just reorder the factor, it makes it an ordered factor.  I don't
> really see why, especially as the modelling functions assume that ordered
> means equally spaced.  If this is to be used more generally (as Kjetil
> Halvorsen suggests) then it should record the scores used to do the
> ordering in an attribute.

Well, the ordered factor issue had come up before, and I currently define this 
as

reorder.factor <-
  function (Factor, X, Function = mean, ..., order = is.ordered(Factor))
    (if (order) ordered else factor)(Factor, 
        levels = names(sort(tapply(X, Factor, Function, ...))))

which is different from the S-PLUS version in 2 ways: 

1. for (unordered) factors, it changes the levels, but keeps it a factor

2. it works for non-factors as well (which is moot if it's going to be 
   a factor method)


Implementation details aside, this seems to me like a good candidate for stats 
(although it'll probably be used very little). lattice functions (will) have 
an alternative way of determining panel order based on panel contents, which 
makes more sense to me in the plotting context.

Deepayan



More information about the R-devel mailing list