在一次调用中对多个变量分组应用几个汇总函数(sum、 mean 等)

我有以下数据框

x <- read.table(text = "  id1 id2 val1 val2
1   a   x    1    9
2   a   x    2    4
3   a   y    3    5
4   a   y    4    9
5   b   x    1    7
6   b   y    4    4
7   b   x    3    9
8   b   y    2    8", header = TRUE)

我想计算 val1和 val2按 id1和 id2分组的平均值,并同时计算每个 id1-id2组合的行数。我可以分别计算:

# calculate mean
aggregate(. ~ id1 + id2, data = x, FUN = mean)


# count rows
aggregate(. ~ id1 + id2, data = x, FUN = length)

为了在一次通话中同时进行两个计算,我尝试了

do.call("rbind", aggregate(. ~ id1 + id2, data = x, FUN = function(x) data.frame(m = mean(x), n = length(x))))

然而,我得到了一个混乱的输出和一个警告:

#     m   n
# id1 1   2
# id2 1   1
#     1.5 2
#     2   2
#     3.5 2
#     3   2
#     6.5 2
#     8   2
#     7   2
#     6   2
# Warning message:
#   In rbind(id1 = c(1L, 2L, 1L, 2L), id2 = c(1L, 1L, 2L, 2L), val1 = list( :
#   number of columns of result is not a multiple of vector length (arg 1)

我可以使用 plyr 包,但是我的数据集非常大,当数据集的大小增长时,plyr 非常慢(几乎不可用)。

如何使用 aggregate或其他函数在一次调用中执行多个计算?

113649 次浏览

Perhaps you want to merge?

x.mean <- aggregate(. ~ id1+id2, p, mean)
x.len  <- aggregate(. ~ id1+id2, p, length)


merge(x.mean, x.len, by = c("id1", "id2"))


id1 id2 val1.x val2.x val1.y val2.y
1   a   x    1.5    6.5      2      2
2   a   y    3.5    7.0      2      2
3   b   x    2.0    8.0      2      2
4   b   y    3.0    6.0      2      2

You can do it all in one step and get proper labeling:

> aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) )
#   id1 id2 val1.mn val1.n val2.mn val2.n
# 1   a   x     1.5    2.0     6.5    2.0
# 2   b   x     2.0    2.0     8.0    2.0
# 3   a   y     3.5    2.0     7.0    2.0
# 4   b   y     3.0    2.0     6.0    2.0

This creates a dataframe with two id columns and two matrix columns:

str( aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) ) )
'data.frame':   4 obs. of  4 variables:
$ id1 : Factor w/ 2 levels "a","b": 1 2 1 2
$ id2 : Factor w/ 2 levels "x","y": 1 1 2 2
$ val1: num [1:4, 1:2] 1.5 2 3.5 3 2 2 2 2
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr  "mn" "n"
$ val2: num [1:4, 1:2] 6.5 8 7 6 2 2 2 2
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr  "mn" "n"

As pointed out by @lord.garbage below, this can be converted to a dataframe with "simple" columns by using do.call(data.frame, ...)

str( do.call(data.frame, aggregate(. ~ id1+id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) ) )
)
'data.frame':   4 obs. of  6 variables:
$ id1    : Factor w/ 2 levels "a","b": 1 2 1 2
$ id2    : Factor w/ 2 levels "x","y": 1 1 2 2
$ val1.mn: num  1.5 2 3.5 3
$ val1.n : num  2 2 2 2
$ val2.mn: num  6.5 8 7 6
$ val2.n : num  2 2 2 2

This is the syntax for multiple variables on the LHS:

aggregate(cbind(val1, val2) ~ id1 + id2, data = x, FUN = function(x) c(mn = mean(x), n = length(x) ) )

You could add a count column, aggregate with sum, then scale back to get the mean:

x$count <- 1
agg <- aggregate(. ~ id1 + id2, data = x,FUN = sum)
agg
#   id1 id2 val1 val2 count
# 1   a   x    3   13     2
# 2   b   x    4   16     2
# 3   a   y    7   14     2
# 4   b   y    6   12     2


agg[c("val1", "val2")] <- agg[c("val1", "val2")] / agg$count
agg
#   id1 id2 val1 val2 count
# 1   a   x  1.5  6.5     2
# 2   b   x  2.0  8.0     2
# 3   a   y  3.5  7.0     2
# 4   b   y  3.0  6.0     2

It has the advantage of preserving your column names and creating a single count column.

Given this in the question :

I could use the plyr package, but my data set is quite large and plyr is very slow (almost unusable) when the size of the dataset grows.

Then in data.table (1.9.4+) you could try :

> DT
id1 id2 val1 val2
1:   a   x    1    9
2:   a   x    2    4
3:   a   y    3    5
4:   a   y    4    9
5:   b   x    1    7
6:   b   y    4    4
7:   b   x    3    9
8:   b   y    2    8


> DT[ , .(mean(val1), mean(val2), .N), by = .(id1, id2)]   # simplest
id1 id2  V1  V2 N
1:   a   x 1.5 6.5 2
2:   a   y 3.5 7.0 2
3:   b   x 2.0 8.0 2
4:   b   y 3.0 6.0 2


> DT[ , .(val1.m = mean(val1), val2.m = mean(val2), count = .N), by = .(id1, id2)]  # named
id1 id2 val1.m val2.m count
1:   a   x    1.5    6.5     2
2:   a   y    3.5    7.0     2
3:   b   x    2.0    8.0     2
4:   b   y    3.0    6.0     2


> DT[ , c(lapply(.SD, mean), count = .N), by = .(id1, id2)]   # mean over all columns
id1 id2 val1 val2 count
1:   a   x  1.5  6.5     2
2:   a   y  3.5  7.0     2
3:   b   x  2.0  8.0     2
4:   b   y  3.0  6.0     2

For timings comparing aggregate (used in question and all 3 other answers) to data.table see this benchmark (the agg and agg.x cases).

Using the dplyr package you could achieve this by using summarise_all. With this summarise-function you can apply other functions (in this case mean and n()) to each of the non-grouping columns:

x %>%
group_by(id1, id2) %>%
summarise_all(funs(mean, n()))

which gives:

     id1    id2 val1_mean val2_mean val1_n val2_n
1      a      x       1.5       6.5      2      2
2      a      y       3.5       7.0      2      2
3      b      x       2.0       8.0      2      2
4      b      y       3.0       6.0      2      2

If you don't want to apply the function(s) to all non-grouping columns, you specify the columns to which they should be applied or by excluding the non-wanted with a minus using the summarise_at() function:

# inclusion
x %>%
group_by(id1, id2) %>%
summarise_at(vars(val1, val2), funs(mean, n()))


# exclusion
x %>%
group_by(id1, id2) %>%
summarise_at(vars(-val2), funs(mean, n()))

You can also use the plyr::each() to introduce multiple functions:

aggregate(cbind(val1, val2) ~ id1 + id2, data = x, FUN = plyr::each(avg = mean, n = length))

After dplyr version 1.0.0, the above summarize_all and summarize_at functions were superseded by summarize(across(...)), where you can select columns to operate on (val1:val2 here).

We can also supply a list of functions in across, and set column names with glue specification ({.col} = original column name, {.fn} = function name in the list).

More information of across can be found in the official documentation.

library(dplyr)


x %>% group_by(id1, id2) %>%
summarize(across(val1:val2, list(mean = mean, n = length), .names = "{.col}_{.fn}"))


# A tibble: 4 × 6
# Groups:   id1 [2]
id1   id2   val1_mean val1_n val2_mean val2_n
<chr> <chr>     <dbl>  <int>     <dbl>  <int>
1 a     x           1.5      2       6.5      2
2 a     y           3.5      2       7        2
3 b     x           2        2       8        2
4 b     y           3        2       6        2