Description Usage Arguments Note Author(s) See Also Examples
Base R's aggregate function allows you to
specify multiple functions when aggregating. However, the
output of such commands is a data.frame where the
aggregated "columns" are actually matrices.
aggregate2 is a basic wrapper around
aggregate that outputs a regular data.frame
instead.
1 | aggregate2(data, aggs, ids, funs = NULL, ...)
|
data |
Your |
aggs |
The variables that need to be aggregated, specified as a character vector. |
ids |
The variables that serve as grouping variables, specified as a character vector. |
funs |
The functions that you want to apply, specified as a character vector. |
... |
Further arguments to |
This function essentially constructs a formula
that can be used with aggregate and keeps track of
the names of the aggregation functions you have applied
to create new variable names. This function is not very
useful when the output of FUN would already output
a matrix (for example, if FUN = fivenum or
FUN = summary). In such cases, it is recommended
to use base R's aggregate with a do.call.
For example: do.call("data.frame", aggregate(. ~
Species, iris, summary)).
Ananda Mahto
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # One-to-one, two functions
(temp1a <- aggregate(weight ~ feed, data = chickwts,
function(x) cbind(mean(x), sum(x))))
str(temp1a)
(temp1b <- aggregate2(chickwts, "weight", "feed", c("mean", "sum")))
str(temp1b)
# Many-to-many, two functions
(temp2a <- aggregate(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph,
function(x) cbind(sum(x), mean(x))))
str(temp2a)
(temp2b <- aggregate2(esoph, c("ncases", "ncontrols"),
c("alcgp", "tobgp"), c("sum", "mean")))
str(temp2b)
# Dot notation
(temp3a <- aggregate(len ~ ., data = ToothGrowth,
function(x) cbind(sum(x), mean(x))))
str(temp3a)
(temp3b <- aggregate2(ToothGrowth, "len", ".", c("sum", "mean")))
str(temp3b)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.