Does aggregate() guarantee that the result will be ordered by the grouping columns?

穿精又带淫゛_ 提交于 2019-12-11 03:57:15

问题


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.


回答1:


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]
...


来源:https://stackoverflow.com/questions/30541130/does-aggregate-guarantee-that-the-result-will-be-ordered-by-the-grouping-colum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!