[Rd] 'aggregate' should preserve level ordering of factors (PR#9666)

prechelt at inf.fu-berlin.de prechelt at inf.fu-berlin.de
Tue May 8 19:12:20 CEST 2007

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

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


?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),
  if (is.factor(dfsubbar.var)) {  # R 2.4: this should be done by 'aggregate'
    df.a$other = factor(df.a$other,


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

  Lutz Prechelt

More information about the R-devel mailing list