From prechelt at inf.fu-berlin.de  Tue May  8 19:12:20 2007
From: prechelt at inf.fu-berlin.de (prechelt at inf.fu-berlin.de)
Date: Tue,  8 May 2007 19:12:20 +0200 (CEST)
Subject: [Rd] 'aggregate' should preserve level ordering of factors (PR#9666)
Message-ID: <20070508171220.71C5A48697@slim.kubism.ku.dk>

Full_Name: Lutz Prechelt
Version: 2.4.1
OS: Windows XP
Submission from: (NULL) (160.45.111.67)


aggregate (from package stats) should preserve the 
ordering of levels of factors it works on and also their
'ordered' attribute if present.
But it does not.

Here is an example:

ff = factor(c("a","b","a","b"),levels=c("b","a"),ordered=T)
agg = aggregate(1:4, list(groups=ff), sum)
print(levels(agg$groups))  # should be: "b" "a"
[1] "a" "b"
print(is.ordered(agg$groups))  # should be: TRUE
[1] FALSE

-----

?aggregate ignores the issue completely:
- the terms 'order' or 'level' do not occur in the
  text at all
- the term 'factor' is mentioned only once:
  "The elements of the list will be coerced to
   factors (if they are not already factors)."

-----

This issue made me write the following code used
for preparing the data for a barchart:

  df.a = aggregate(df[,value.var],
                   list(grouping=dfgrouping, other=dfsubbar.var),
                   FUN=FUN)
  if (is.factor(dfsubbar.var)) {  # R 2.4: this should be done by 'aggregate'
    df.a$other = factor(df.a$other,
                        levels=levels(dfsubbar.var),
                        ordered=is.ordered(dfsubbar.var))
  }

Cumbersome.

R is great anyway. Thanks for your service building it!

  Lutz Prechelt



[Rd] 'aggregate' should preserve level ordering of factors (PR#9666)
prechelt at inf.fu-berlin.de