2

I've noticed that aggregate() appears to return its result ordered by the grouping column(s). Is this a guarantee? Can this be relied upon in surrounding logic?

A couple of examples:

set.seed(1); df <- data.frame(group=sample(letters[1:3],10,replace=T),value=1:10);
aggregate(value~group,df,sum);
##   group value
## 1     a    16
## 2     b    22
## 3     c    17

And with two groups (notice the second group is ordered first, then the first group to break ties):

set.seed(1); df <- data.frame(group1=sample(letters[1:3],10,replace=T),group2=sample(letters[4:6],10,replace=T),value=1:10);
aggregate(value~group1+group2,df,sum);
##   group1 group2 value
## 1      a      d     1
## 2      b      d     2
## 3      b      e     9
## 4      c      e    10
## 5      a      f    15
## 6      b      f    11
## 7      c      f     7

Note: I'm asking because I just came up with an answer for Aggregating while merging two dataframes in R which, at least in its current form at the time of writing, depends on aggregate() returning its result ordered by the grouping column.

Community
  • 1
  • 1
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • Yeah, I've wondered that, too. You could have a look at the source code by typing `aggregate.data.frame` or similar in the console. I do see a `sort` in there. – Frank May 30 '15 at 01:17
  • Note that if you reverse the order in the formula (`value~group2+group1`), you get the opposite ordering. – nograpes May 30 '15 at 01:50

1 Answers1

2

Yes, as long as you understand the natural ordering of factors to be by their integer keys. You can see this in the code:

y <- as.data.frame(by, stringsAsFactors = FALSE)
...  # y becomes the "integerized" dataframe of index vectors
grp <- rank(do.call(paste, c(lapply(rev(y), ident), list(sep = "."))), 
        ties.method = "min")
y <- y[match(sort(unique(grp)), grp, 0L), , drop = FALSE]
...
IRTFM
  • 258,963
  • 21
  • 364
  • 487